data.table | .SD在`...`-Argument中加载多个列 [英] data.table | .SD lapply multiple columns in `...`-Argument

查看:103
本文介绍了data.table | .SD在`...`-Argument中加载多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用固定的特定列 ... 参数 lapply(.SD,FUN,...)

How can i use a fixed specific column the ... argument of 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]

Is there a better way of doing it? Without referencing to y by y=DT[,id_column]

推荐答案

问题#495 现在解决了此最近提交,我们现在可以这样做:

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

这篇关于data.table | .SD在`...`-Argument中加载多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆