如何找到分组均值并将其保存在R中的数据框对象中?
在数据分析中,我们经常需要按组均值,特别是在使用方差分析的情况下,因为这些技术可以帮助我们根据中心趋势和变化量度来比较不同的群体。可以通过使用聚合函数来完成,以便可以将输出保存在数据框对象中。在下面的示例中,我们可以看到如何完成操作以及检查最终对象类型。
示例
请看以下数据帧-
set.seed(109) Salary<-sample(10000:20000,20) Group<-sample(c("High-school","Graduate","Post-Graduate"),20,replace=TRUE) df1<-data.frame(Group,Salary) df1
输出结果
Group Salary 1 Graduate 10250 2 High-school 14923 3 High-school 18928 4 High-school 19800 5 Graduate 15974 6 High-school 16270 7 Post-Graduate 19832 8 Graduate 19246 9 Graduate 11699 10 Graduate 17424 11 High-school 14875 12 Post-Graduate 12319 13 Post-Graduate 16900 14 High-school 12361 15 Post-Graduate 15809 16 Post-Graduate 19854 17 High-school 14387 18 High-school 13647 19 Graduate 18587 20 Graduate 11817
用聚合函数找到分组均值-
Groupwise_mean<-aggregate(df1$Salary,list(df1$Group),mean) Groupwise_mean Group.1 x 1 Graduate 14924.60 2 High-school 16524.57 3 Post-Graduate 17362.67
检查对象Groupwise均值是否是数据帧-
is.data.frame(Groupwise_mean) [1] TRUE
让我们看另一个例子-
示例
Class<-rep(LETTERS[1:4],times=5) Age<-sample(19:30,20,replace=TRUE) df2<-data.frame(Class,Age) df2
输出结果
Class Age 1 A 29 2 B 22 3 C 26 4 D 20 5 A 28 6 B 21 7 C 19 8 D 24 9 A 29 10 B 30 11 C 23 12 D 25 13 A 21 14 B 21 15 C 21 16 D 20 17 A 21 18 B 24 19 C 19 20 D 21
>Groupwise_mean_Age<-aggregate(df2$Age,list(df2$Class),mean)
>Groupwise_mean_Age
1组
Group.1x 1 A 24.8 2 B 25.2 3 C 25.2 4 D 24.0 > is.data.frame(Groupwise_mean_Age) [1] TRUE