我想对 data.table 中的一些列求和,在变量中指定这些列。然后我使用..
前缀(请参阅新功能data.table
1.10.2)来选择这些列。但是,这会导致警告:
mdt <- as.data.table(mtcars)
factorsGEN <- c("disp","hp","drat")
# This works but gives the warning below
mdt[ , score := rowSums(mdt[ , ..factorsGEN])]
#Warning message:
# In `[.data.table`(mdt, , ..factorsGEN) :
# Both 'factorsGEN' and '..factorsGEN' exist in calling scope. Please remove
# the '..factorsGEN' variable in calling scope for clarity.
# This does not work, results in error because factorsGEN is not found
mdt[, score := rowSums(mdt[, factorsGEN])]
Run Code Online (Sandbox Code Playgroud)
我收到一条警告,我不记得我第一次编写代码时收到的警告,所以它可能是更新 data.table 代码的结果。谁能告诉我如何避免警告。我想不通。
小智 1
您可以使用 .SD,警告就会消失。
mdt[ , score := rowSums(mdt[ , .SD, .SDcols = factorsGEN])]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
566 次 |
最近记录: |