How to create new column in a data.table when the name of column must be a string

Viewed 11077

How to create new column in a data.table when the name of column must be a string or character?

For example:

library(data.table)
DT = data.table(v1=c(1,2,3), v2=2:4)
new_var <- "v3"
DT[, new_var:=v2+5]

I Get

DT
#>    v1 v2 new_var
#> 1:  1  2       7
#> 2:  2  3       8
#> 3:  3  4       9

But, I want

#>    v1 v2      v3
#> 1:  1  2       7
#> 2:  2  3       8
#> 3:  3  4       9
1 Answers
Related