DT <- data.table(x = 1:5, y = 1:5, z = 6:10)
DT[ , c(list(x), lapply(.SD, `^`, 2)), .SDcols = c("y", "z")]
# V1 y z
#1: 1 1 36
#2: 2 4 49
#3: 3 9 64
#4: 4 16 81
#5: 5 25 100
Is conjugate to the approach without .SD:
DT[ , .(x, y^2, z^2)]
# x V2 V3
#1: 1 1 36
#2: 2 4 49
#3: 3 9 64
#4: 4 16 81
#5: 5 25 100
Seems like it's clear in the first case that V1 should be named x (fixed with c(list(x=x, ...)))
Is conjugate to the approach without
.SD:Seems like it's clear in the first case that
V1should be namedx(fixed withc(list(x=x, ...)))