R data.table applies a function to the sum of two columns
I have a data.table and I would like to apply a function on its columns. This is usually done as follows:
dt[, lapply(.SD, func), .SDcols = c("col1", "col2")]
And this will apply the function func
to those two columns. What if, however, I would like to apply it on the sum of these two columns? Something like
dt[, lapply(.SD, func), .SDcols = "col1 + col2"]
obviously doesn't work.
You can generalize this to apply func
another function (in this case, sum
) to the result that takes columns as arguments. I know I can create another column containing the results of the first function, but is there a way around this?
+3
source to share