Multiple functions on multiple columns by group, and create informative column names

后端 未结 5 866
心在旅途
心在旅途 2020-12-16 19:17

How to adjust a data table manipulation so that, besides sum per category of several colums, it would also calculate other functions at the same time such as <

5条回答
  •  情歌与酒
    2020-12-16 20:07

    Consider building a list of data tables where you iterate through every ColChoice and apply each function of FuncChoice (setting names accordingly). Then, to merge all data tables together, run merge in a Reduce call. Also, use get to retrieve environment objects (functions/columns).

    Note: ColChoice was renamed for camel case and length function replaces .N for functional form for count:

    set.seed(12212018)  # RUN BEFORE data.table() BUILD TO REPRODUCE OUTPUT
    ...
    
    ColChoice <- c("c1", "c4")
    FunChoice <- c("length", "mean", "sum")
    
    output <- lapply(ColChoice, function(col)
                       dt[, setNames(lapply(FunChoice, function(f) get(f)(get(col))), 
                                     paste0(col, "_", FunChoice)), 
                          by=category]
              )
    
    final_dt <- Reduce(function(x, y) merge(x, y, by="category"), output)
    
    head(final_dt)
    
    #    category c1_length   c1_mean   c1_sum c4_length   c4_mean   c4_sum
    # 1:        a      3893 10000.001 38930003      3893  9.990517 38893.08
    # 2:        b      4021 10000.028 40210113      4021  9.977178 40118.23
    # 3:        c      3931 10000.008 39310030      3931  9.996538 39296.39
    # 4:        d      3954 10000.010 39540038      3954 10.004578 39558.10
    # 5:        e      4016  9999.998 40159992      4016 10.002131 40168.56
    # 6:        f      3974  9999.987 39739947      3974  9.994220 39717.03
    

提交回复
热议问题