作者:用户tznpaynnl5 | 来源:互联网 | 2023-05-17 17:36
Iwouldliketoapplyafunctiontoallcolumnindata.table.Hence,Iuse.SDwithlapply.But,ins
I would like to apply a function to all column in data.table. Hence, I use .SD with lapply. But, inside lapply I cannot retrieve the column of my table.
我想将一个函数应用于data.table中的所有列。因此,我使用.SD与lapply。但是,在lapply内部,我无法检索我的表的列。
For instance
例如
x = data.table(a=1:10, b=10:1, id=1:5)
x[,lapply(.SD,function(t){t*id}),.SDcols=c(1,2)]
Error in ..FUN(a) : object 'id' not found
I do the following:
我做了以下事情:
x[,lapply(.SD,function(t){t*x$id}),.SDcols=c(1,2)]
Can we do better?
我们可以做得更好吗?
1 个解决方案