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

前端 未结 7 1499
一个人的身影
一个人的身影 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:16

    You can also use the plyr::each() to introduce multiple functions:

    aggregate(cbind(val1, val2) ~ id1 + id2, data = x, FUN = plyr::each(avg = mean, n = length))
    

提交回复
热议问题