I am looking for a way to use the split-apply-combine strategy with R's data.table
package.
library(data.table)
# take a data.table object, return integer
func <- function(DT)
{
DT$a * DT$a
}
DT = data.table(
a = 1:50
# ... further fields here
b = rep(1:10, 5)
)
# this obviously won't work:
DT[, result:=func, by=b]
# but this will (based on @Aruns answer below)
DT[, result:=func(.SD), by=b]
While this here is very simple data.table
, with more complicated structures, I'd like to be able to extract logic into functions and send subsets as data.table
s to them, without having to enlist all field names.