This may be very elementary question. I wanted to create a new variable in existing data.table object, and wanted to save the new object with different name. However, I am facing a problem:
library(data.table)
DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
a <- colnames(DT)[-1] #variables to be modified
a.va <- paste(a, "x", sep = "_") #new variables name
DT2 <- DT[, (a.va) := lapply(.SD, FUN = function(x) (sum(x, na.rm = T)-x) / (.N - 1)), .SDcols = a, by = x]
However, it changes the both existing object DT as well. I wanted to keep the original object DT as intact. In my original data set, there are more than 100 variables, so I can not create one by one separate variable.