为什么我无法访问.SDcol中未指定的data.table的变量?

时间:2014-04-01 21:53:37

标签: r data.table

我们是否有理由不能使用J中的变量,我们通过.SDcols将.SD排除在外?

setseed(1)
nobs <- 100
x <- replicate(5, rnorm(nobs),simplify=FALSE)
setDT(x)
x[, eval(paste0("logicalV",1:2)) := replicate(2, as.logical(rbinom(nobs,1,.5)),simplify=FALSE)]


x[,data.table(.SD, logicalV1),.SDcols=-c("logicalV1","logicalV2")] #this doesn't work
x[,data.table(.SD, x$logicalV1),.SDcols=-c("logicalV1","logicalV2")] #have to specify x$


x[(logicalV2),apply(.SD,2,`*`,logicalV1),.SDcols=-c("logicalV1","logicalV2")] #it would be nice if this worked
x[(logicalV2),apply(.SD,2,`*`,x$logicalV1[x$logicalV2]),.SDcols=-c("logicalV1","logicalV2")] #need to write the i statement twice

0 个答案:

没有答案