我正在尝试创建一个分组摘要,报告每个组中的记录数,然后还显示一系列变量的平均值。
我只能将如何做到这一点作为两个单独的摘要,然后将它们合并在一起。这很好用,但我想知道是否有更优雅的方法来做到这一点?
dailyn<-daily %>% # this summarises n
group_by(type) %>%
summarise(n=n()) %>%
dailymeans <- daily %>% # this summarises the means
group_by(type) %>%
summarise_at(vars(starts_with("d.")),funs(mean(., na.rm = TRUE))) %>%
dailysummary<-inner_join(dailyn,dailymeans) #this joins the two parts together
我正在使用的数据是这样的数据框:
daily<-data.frame(type=c("A","A","B","C","C","C"),
d.happy=c(1,5,3,7,2,4),
d.sad=c(5,3,6,3,1,2))