data.table | .SD lapply `...` 中的多列 - 参数

data.table | .SD lapply multiple columns in `...`-Argument

如何在 lapply(.SD, FUN, ...)

... 参数中使用固定的特定列

例子

DT <- data.table(id_column = rnorm(10),
                 x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
measure_col = paste0("x",1:3)

DT[,lapply(.SD, cov, y=id_column), .SDcols = measure_col]

结果

Error in is.data.frame(y) : object 'id_column' not found

可能的解决方法是

DT[,lapply(.SD, cov, y = DT[,id_column]), .SDcols = measure_col]
          x1         x2        x3
1: 0.1703253 -0.2831533 0.3387133

有更好的方法吗?没有通过 y=DT[,id_column]

引用 y

问题 #495 is solved now with this recent commit,我们现在可以做到这一点:

require(data.table) # v1.9.7+
DT <- data.table(id_column = rnorm(10), x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
measure_col = paste0("x",1:3)    
DT[,lapply(.SD, cov, y=id_column), .SDcols = measure_col]
#             x1        x2         x3
# 1: -0.03137294 0.1903654 -0.1493648