R语言 如何使用聚合函数
在这篇文章中,我们将讨论如何在R编程语言中使用聚合函数。
aggregate()函数用于按组获得数据的汇总统计。这些统计数据包括平均值、最小值、总和、最大值等。
语法:
aggregate(dataframeaggregate_column, list(dataframegroup_column), FUN)
其中
- dataframe是输入的数据框。
- aggreg_column是数据框架中要聚合的列。
- group_column是要用FUN分组的列。
- FUN代表sum/mean/min/max。
例子1 :R程序创建了4个列,用subject分组,并得到最小、总和、最大等聚合值。
# create a dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# display
print(data)
# aggregate sum of marks with subjects
print(aggregate(datamarks, list(datasubjects), FUN=sum))
# aggregate minimum of marks with subjects
print(aggregate(datamarks, list(datasubjects), FUN=min))
# aggregate maximum of marks with subjects
print(aggregate(datamarks, list(datasubjects), FUN=max))
输出:
例2: 用R程序创建4个列,用受试者分组,得到平均数(平均值)。
# create a dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# display
print(data)
# aggregate average of marks with subjects
print(aggregate(datamarks, list(datasubjects), FUN=mean))
输出: