'Confused by warning when using the "dot dot prefix" (..) in data.table
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.
Solution 1:[1]
You can use .SD and the warning vanishes.
mdt[ , score := rowSums(mdt[ , .SD, .SDcols = factorsGEN])]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Richard Layton |
