首页 文章

dplyr按组和总和分开

提问于
浏览
0

我试图通过两个值(名称和p23)对数据进行排序,然后对p23的类似值求和 . 我还是dplyr的新手,我不知道下一步该做什么

install.packages("dplyr")
library(dplyr)
# library(tidyr)

df<-data.frame(
  name=c("a","b","b","c","b"),
  p23=c(2,2,3,3,2),
  data=c(0,1,2,3,4)
)
res<-df %>% group_by(name,p23) #now what?

目标

a 2 0    #this is also an average but there is only one value
b 2 2.5  #this is the average of the two b's in p23 with value 2
b 3 2
c 3 3

1 回答

  • 2
    df %>% group_by(name,p23)  %>% summarise(mean(data))
    Source: local data frame [4 x 3]
    Groups: name
    
      name p23 mean(data)
    1    a   2        0.0
    2    b   2        2.5
    3    b   3        2.0
    4    c   3        3.0
    

相关问题