Apply several summary functions on several variables by group in one call

前端 未结 7 1466
一个人的身影
一个人的身影 2020-11-22 00:03

I have the following data frame

x <- read.table(text = \"  id1 id2 val1 val2
1   a   x    1    9
2   a   x    2    4
3   a   y    3    5
4   a   y    4            


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 00:05

    Using the dplyr package you could achieve this by using summarise_all. With this summarise-function you can apply other functions (in this case mean and n()) to each of the non-grouping columns:

    x %>%
      group_by(id1, id2) %>%
      summarise_all(funs(mean, n()))
    

    which gives:

         id1    id2 val1_mean val2_mean val1_n val2_n
    1      a      x       1.5       6.5      2      2
    2      a      y       3.5       7.0      2      2
    3      b      x       2.0       8.0      2      2
    4      b      y       3.0       6.0      2      2
    

    If you don't want to apply the function(s) to all non-grouping columns, you specify the columns to which they should be applied or by excluding the non-wanted with a minus using the summarise_at() function:

    # inclusion
    x %>%
      group_by(id1, id2) %>%
      summarise_at(vars(val1, val2), funs(mean, n()))
    
    # exclusion
    x %>%
      group_by(id1, id2) %>%
      summarise_at(vars(-val2), funs(mean, n()))
    

提交回复
热议问题