I am exploring different ways to wrap an aggregation function (but really it could be any type of function) using data.table (one dplyr example is also provided) and was wonderi
I don't recommend eval(parse())
. You can achieve the same as in approach three without it:
fn_dt_agg4 <-
function(dt, metric, metric_name, dimension, dimension_name, agg_type) {
e <- function(x) getFunction(agg_type)(x, na.rm = T)
temp <- dt[, setNames(lapply(.SD, e),
metric_name),
keyby = dimension, .SDcols = metric]
temp[]
}
This also avoids some security risks.
PS: You can check what data.table is doing regarding optimizations by setting options("datatable.verbose" = TRUE)
.