Using `car` to recode across range of columns
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
Sure you can. In fact doing only on a subset of the data.frame lets you avoid having to redo the data.frame call:
df_2[ , col_names] <- lapply(df[ ,colnames] ,
FUN = function(x) recode(x, "2=1;1=0;0=NA"))
Of do it by col-number:
df_2[ , 20:40] <- lapply(df[ ,20:40] ,
FUN = function(x) recode(x, "2=1;1=0;0=NA"))