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

后端 未结 5 872
心在旅途
心在旅途 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 19:55

    It seems that there's not a straightforward answer using data.table since noone has answered this yet. So I'll propose a dplyr-based answer that should do what you want. I use the built-in iris data set for the example:

    library(dplyr)
    iris %>% 
       group_by(Species) %>% 
      summarise_at(vars(Sepal.Length, Sepal.Width), .funs = c(sum=sum,mean= mean), na.rm=TRUE)
    
    ## A tibble: 3 x 5
    #  Species    Sepal.Length_sum Sepal.Width_sum Sepal.Length_mean Sepal.Width_mean
    #                                                       
    #1 setosa                 245.            171.              5.00             3.43
    #2 versicolor             297.            138.              5.94             2.77
    #3 virginica              323.            149.              6.60             2.97
    

    or using character vector input for the columns and functions:

    Colchoice <- c("Sepal.Length", "Sepal.Width")
    FunChoice <- c("mean", "sum")
    iris %>% 
      group_by(Species) %>% 
      summarise_at(vars(Colchoice), .funs = setNames(FunChoice, FunChoice), na.rm=TRUE)
    ## A tibble: 3 x 5
    #  Species    Sepal.Length_mean Sepal.Width_mean Sepal.Length_sum Sepal.Width_sum
    #                                                       
    #1 setosa                  5.00             3.43             245.            171.
    #2 versicolor              5.94             2.77             297.            138.
    #3 virginica               6.60             2.97             323.            149.
    

提交回复
热议问题