rdata.table

Apply a function to every specified column in a data.table and update by reference


I have a data.table with which I'd like to perform the same operation on certain columns. The names of these columns are given in a character vector. In this particular example, I'd like to multiply all of these columns by -1.

Some toy data and a vector specifying relevant columns:

library(data.table)
dt <- data.table(a = 1:3, b = 1:3, d = 1:3)
cols <- c("a", "b")

Right now I'm doing it this way, looping over the character vector:

for (col in 1:length(cols)) {
   dt[ , eval(parse(text = paste0(cols[col], ":=-1*", cols[col])))]
}

Is there a way to do this directly without the for loop?


Solution

  • This seems to work:

    dt[ , (cols) := lapply(.SD, "*", -1), .SDcols = cols]
    

    The result is

        a  b d
    1: -1 -1 1
    2: -2 -2 2
    3: -3 -3 3
    

    There are a few tricks here:

    EDIT: Here's another way that is probably faster, as @Arun mentioned:

    for (j in cols) set(dt, j = j, value = -dt[[j]])