Reputation: 504
I want to sum some columns in a data.table, specifying those columns in a variable. I then use the ..
prefix (see New Features data.table
1.10.2) to select those columns. However, this results in a Warning:
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])]
I get a warning, which I don't remember getting when I first wrote the code, so it may be the result of an update to the data.table code. Could anyone please tell me how to avoid the warning. I can't figure it out.
Upvotes: 6
Views: 1290
Reputation: 126
You can use .SD and the warning vanishes.
mdt[ , score := rowSums(mdt[ , .SD, .SDcols = factorsGEN])]
Upvotes: 1