Search code examples
rpackageradix

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 packages? 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.


Solution

  • 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.