How can i use a fixed specific column the ...
argument of lapply(.SD, FUN, ...)
Example
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]
Results in
Error in is.data.frame(y) : object 'id_column' not found
A possible workaround would be
DT[,lapply(.SD, cov, y = DT[,id_column]), .SDcols = measure_col]
x1 x2 x3
1: 0.1703253 -0.2831533 0.3387133
Is there a better way of doing it? Without referencing to y by y=DT[,id_column]
Issue #495 is solved now with this recent commit , we can now do this just fine:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.