I have a simple DT and I would like to add a column to the rest. The code is as follows: (works)
x <- data.table(a=1:5,b=5:1,c=rep(999,5))
for(k in c("a","b")){x[,k] <- x[,..k]+x[,.(c)]}
Now here is the question: Why do I have to use ..
for the assignment? Also if I try to use ..
in the first case, i.e.
for(k in c("a","b")){x[,..k] <- x[,..k]+x[,.(c)]}
There is an error: "[...]object '..k' not found". This seems strange, that I have to change the syntax within the scope.
Now in dataframe
, the equivalent formulation is very clear:
for(k in c("a","b")){x[,k] <- x[,k]+x[,c]} # error with DT
x <- data.frame(a=1:5,b=5:1,c=rep(999,5))
for(k in c("a","b")){x[,k] <- x[,k]+x[,"c"]} # works with dataframe
So I am wondering (1) if the above code is the correct way to do that in datatable
(please explain the ..
operator, the datatable FAQ 1.1 doesn't address this in particular); and if (2) there are alternative ways to write this in a cleaner way. Thanks for any hints.