2

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.

MatthewR
  • 2,660
  • 5
  • 26
  • 37

1 Answers1

4

IIUC, you are adding new columns. The idiomatic way is to use := as follows, by putting the new character vector of column names within brackets () before :=

library(data.table)
mtc <- data.table(mtcars)
cols <- c("hp", "drat", "gear")
mtc[, (paste0(cols, "_newvars")) := lapply(.SD, `*`, wt), .SDcols=cols]
mtc

In addition, you do not need to access wt column using [[ within j (see ?data.table for definition of j)

chinsoon12
  • 25,005
  • 4
  • 25
  • 35