Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `car` to recode across range of columns

I've been poking about on the internet, and can't figure out how to apply car to recode values for a range of columns.

To recode values for a single column, I'd run a command such as:

 df$dv_r <- recode(df$dv, "2=1;1=0;0=NA")

And then if I wanted to do this for the whole data.frame, I could run:

 df_2 <- lapply(df, FUN = function(x) recode(x, "2=1;1=0;0=NA"))

However, I'm not sure how to do this for a range of columns -- for example, in a hypothetical data.table called df, how would I recode values for columns ranging from 20:40?

Thanks! Sure this is super easy for R experts.

like image 598
roody Avatar asked Apr 23 '13 21:04

roody


1 Answers

Perhaps there is a more data.table way to do this, but here is one possibility:

library(data.table)
library(car)

## Here is some sample data
set.seed(1)
dt <- data.table(A = sample(0:2, 10, replace = TRUE), 
                 B = sample(0:2, 10, replace = TRUE),
                 C = sample(0:2, 10, replace = TRUE),
                 D = rnorm(10), E = rnorm(10), ID = 1:10)
dt
#     A B C           D           E ID
#  1: 0 0 2 -0.04493361 -0.05612874  1
#  2: 1 0 0 -0.01619026 -0.15579551  2
#  3: 1 2 1  0.94383621 -1.47075238  3
#  4: 2 1 0  0.82122120 -0.47815006  4
#  5: 0 2 0  0.59390132  0.41794156  5
#  6: 2 1 1  0.91897737  1.35867955  6
#  7: 2 2 0  0.78213630 -0.10278773  7
#  8: 1 2 1  0.07456498  0.38767161  8
#  9: 1 1 2 -1.98935170 -0.05380504  9
# 10: 0 2 1  0.61982575 -1.37705956 10

Use .SDcols to define which columns you want to apply the function to.

dt[, 1:3 := lapply(.SD, recode, "2=1;1=0;0=NA"), .SDcols = 1:3]
dt
#      A  B  C           D           E ID
#  1: NA NA  1 -0.04493361 -0.05612874  1
#  2:  0 NA NA -0.01619026 -0.15579551  2
#  3:  0  1  0  0.94383621 -1.47075238  3
#  4:  1  0 NA  0.82122120 -0.47815006  4
#  5: NA  1 NA  0.59390132  0.41794156  5
#  6:  1  0  0  0.91897737  1.35867955  6
#  7:  1  1 NA  0.78213630 -0.10278773  7
#  8:  0  1  0  0.07456498  0.38767161  8
#  9:  0  0  1 -1.98935170 -0.05380504  9
# 10: NA  1  0  0.61982575 -1.37705956 10
like image 97
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 23 '22 01:09

A5C1D2H2I1M1N2O1R2T1