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

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

    Perhaps you want to merge?

    x.mean <- aggregate(. ~ id1+id2, p, mean)
    x.len  <- aggregate(. ~ id1+id2, p, length)
    
    merge(x.mean, x.len, by = c("id1", "id2"))
    
      id1 id2 val1.x val2.x val1.y val2.y
    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
    

提交回复
热议问题