也许有更多的data.table
方法可以做到这一点,但这里有一种可能性:
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
用于.SDcols
定义要将函数应用到的列。
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