假设我有一个像这样的数据框:
df
amount group
1 2.327684 1
2 1.351609 1
3 1.532830 2
4 2.135562 2
5 2.847107 2
6 3.585103 2
7 4.375418 2
8 4.138450 2
9 5.929983 2
10 5.178621 2
11 6.756757 2
12 4.749200 2
13 2.208296 2
14 2.244003 2
15 2.016025 2
16 1.698015 3
17 1.459854 3
18 3.350970 3
我的意思是:
> mean(df$amount[df$group==1])
[1] 1.839646
如何计算每个组的平均值并将其作为列附加到df?我看到了这篇帖子How to calculate the mean of ratings of each user?
但是我不理解IPython,我尝试了一下,这导致R崩溃:
#find the means for each group
library(dplyr)
means <-
df1 %>%
group_by(df$group) %>%
#calculate the means
summarise(mean = mean(df$amount))