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

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

ruby - Net::HTTP extremely slow responses for HTTPS requests -