如何在 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 mean 是否为数据框 -
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
Group.1 x
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
广告