R data.table apply function to sum of two columns -


i have data.table, , i'd apply function on columns. done so:

dt[, lapply(.sd, func), .sdcols = c("col1", "col2")] 

and apply function func on 2 columns. if, however, i'd apply on sum of 2 columns? like

dt[, lapply(.sd, func), .sdcols = "col1 + col2"] 

obviously doesn't work.

you generalise applying func result of function (in case, sum) takes in columns arguments. know can create column containing results of first function, there way around that?

to add columns, try

dt[, func(reduce(`+`,.sd)), .sdcols = c("col1","col2")] 

this works more 2 columns well, adding them before applying func.


Comments