r data.table functional programming / metaprogramming / computing on the language

后端 未结 1 1814
陌清茗
陌清茗 2021-02-03 14:41

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

相关标签:
1条回答
  • 2021-02-03 15:00

    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).

    0 讨论(0)
提交回复
热议问题