library(data.table)
new_col <- "test_col"
DT <- data.table(x=1:3)
This works:
DT[, eval(new_col) := x][]
This also works:
DT[, ':=' ("test_col" = x)][]
But this throws "Error: unexpected '=' in "DT[, := (eval(new_col) =" :
DT[, `:=` (eval(new_col) = x)][]
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices
[4] utils datasets methods
[7] base
other attached packages:
[1] data.table_1.13.6
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5
[2] compiler_4.0.3
[3] prettyunits_1.1.1
[4] remotes_2.2.0
[5] tools_4.0.3
[6] testthat_2.3.2
[7] digest_0.6.27
[8] pkgbuild_1.2.0
[9] pkgload_1.1.0
[10] evaluate_0.14
[11] jsonlite_1.7.0
[12] memoise_1.1.0
[13] lattice_0.20-41
[14] rlang_0.4.10
[15] Matrix_1.2-18
[16] cli_2.2.0
[17] rstudioapi_0.13
[18] yaml_2.2.1
[19] xfun_0.19
[20] withr_2.3.0
[21] knitr_1.30
[22] desc_1.2.0
[23] fs_1.5.0
[24] devtools_2.3.1
[25] rprojroot_2.0.2
[26] grid_4.0.3
[27] reticulate_1.16
[28] glue_1.4.2
[29] R6_2.5.0
[30] processx_3.4.5
[31] fansi_0.4.1
[32] rmarkdown_2.6
[33] librarian_1.7.2
[34] sessioninfo_1.1.1
[35] callr_3.4.3
[36] magrittr_2.0.1
[37] htmltools_0.5.0.9003
[38] ps_1.5.0
[39] ellipsis_0.3.1
[40] usethis_1.6.1
[41] assertthat_0.2.1
[42] crayon_1.3.4
This works:
This also works:
But this throws
"Error: unexpected '=' in "DT[,:=(eval(new_col) =":