I understand how to use .SD to perform an operation on multiple columns; I just don't know how to assign those new values to new variables in my data table (not overwriting the input variables)
In this example I create three new vars ending with the string "newvars". I than merge those new vars back on to the data table. Is it possible to just create the new vars without the merge?
mtcars <- data.table( mtcars )
newvars <- mtcars[ , lapply( .SD , function( Z ) Z * mtcars[[ "wt" ]]) , .SDcols= c("hp","drat","gear")]
colnames( newvars ) <- paste( colnames( newvars) , "newvars", sep="_")
mtcars <- cbind( mtcars , newvars )
Seems like there should be a straight forward way of doing this.
If I didn't have so many columns I would just do them one by one
mtcars[ , hp_new := hp*wt ]
Thanks.