总均值R中具有dplyr的组的均值 [英] Total Mean & Mean by groups in R with dplyr

查看:103
本文介绍了总均值R中具有dplyr的组的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似

df <- data.frame(dive=factor(sample(c("dive1","dive2"),10,replace=TRUE)),speed=runif(10))

现在,我的目标是在同一数据中找到数据的总平均值"和"R中的子组平均值".所以,我可以说我应该得到

Now my goal is to find " Total mean of the data" and "Mean by Subgroups in R" in same data. So, I can say I should get something like

#    dive  Total_Mean   speed
# 1 dive1   0.52        0.5790946
# 2 dive2   0.52        0.4864489

我正在使用代码

df%>% summarise(avg=mean(speed))%>%
group_by(dive)%>%
summarise(Avg_group=mean(dive))

我知道错了什么,所以我要寻找的是如何在dplyr中分组并打开数据增益以在不同的时间执行不同的操作

Which is wrong I know, So all I am seeking is how can I group by and open my data gain in dplyr for performing different operations at different time

推荐答案

尝试一下:

df %>% 
   mutate(avg=mean(speed)) %>% 
   group_by(dive) %>% 
   summarise(Avg_group=mean(speed),Total_Mean=first(avg))

这篇关于总均值R中具有dplyr的组的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆