I am using data.table
v. 1.12.8. I am trying to refer to columns with variables, using ()
and get()
. It fails, and I cannot understand why. This kind of syntax does work when using :=
and updating by reference (see this or this question).
library(data.table)
X <- data.table(id = 1:5, group = c(1,1,2,2,2), v1 = c(10,12,14,16,18), type_v1 = c("t1","t2","t1","t1","t2"))
this_var <- "v1"
X[, .("v1" = sum(v1)), by = group] # this works
X[, .((this_var) = sum(get(this_var))), by = group] # this does not
I know that for my example, I could just use .SD
, but my use requires referring to columns dynamically and individually, without updating the original data.table.