开发者

Setting range of values to NA in multiple columns (limited to base and car package)

How can I do some sort of mass recode of a dataset in R using only the base, car, and foreign pac开发者_开发百科kages? I'm on a government computer, so I can't install any additional packages to make this easier. Ideally, I'd like to be able to supply a dataframe to car's recode function or apply the recode function over all/most variables in the dataframe. I'm trying to figure ut how to do this an apply/lapply function, but I don't know how to use them and haven't had much success. the idea looks like the following, though this code doesn't work:

for vectors 2 through 92 in data frame "df":

df<-recode(df[2:92], '98:100=NA)

Any suggestions? I'd greatly appreciate it.


Perhaps:

is.na( df[, 2:92] ) <- df[ ,2:92] >= 98 & df[, 2:92] <= 100

is.na() can accept assignment in which case it is the is.na<- function and needs a logical vector, matrix, or array of the same extent as the target. I tried the %in% function but it was not properly vectorized (or perhaps matricized?) to do the job. I thought maybe just using column numbers as Joe tried would work but also got no success down that path.


I'm going to interpret your question as trying to change a subset of columns in a subset of rows.

Try:

df[c(2:92), c(98:100)] <- NA

You can subset columns just like you can subset rows and assign a value to them.

Edit: This question is addressed in here

To just do a subset of the dataframe:

df2 <- within(df[2:92,], a <- recode(a, 'c("a","b","c")="a"'))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜