在 R 编程中计算子集的汇总统计信息 - 聚合()函数
在 R 编程中, aggregate()
函数用于计算拆分数据的汇总统计信息。它采用数据框或时间序列分析对象。
Syntax: aggregate(x, by, FUN)
Parameters:
x: specifies R object
by: specifies list of grouping elements
FUN: specifies function to compute the statistical summary
要了解更多可选参数,请在控制台中使用以下命令:
help("aggregate")
示例 1:
# Using state.x77 and state.region dataset
# Compute the mean of the states in state.x77
# grouped by state.region variables
aggregate(state.x77, list(region = state.region), mean)
输出:
region Population Income Illiteracy Life Exp Murder HS Grad Frost Area
1 Northeast 5495.111 4570.222 1.000000 71.26444 4.722222 53.96667 132.7778 18141.00
2 South 4208.125 4011.938 1.737500 69.70625 10.581250 44.34375 64.6250 54605.12
3 North Central 4803.000 4611.083 0.700000 71.76667 5.275000 54.51667 138.8333 62652.00
4 West 2915.308 4702.615 1.023077 71.23462 7.215385 62.00000 102.1538 134463.00
示例 2:
# Using mtcars dataset
# Compute the mean of all columns in mtcars
# grouped by gears
aggregate(mtcars, list(mtcars$gears), mean)
输出:
Group.1 mpg cyl disp hp drat wt qsec vs am gear carb
1 3 16.10667 7.466667 326.3000 176.1333 3.132667 3.892600 17.692 0.2000000 0.0000000 3 2.666667
2 4 24.53333 4.666667 123.0167 89.5000 4.043333 2.616667 18.965 0.8333333 0.6666667 4 2.333333
3 5 21.38000 6.000000 202.4800 195.6000 3.916000 2.632600 15.640 0.2000000 1.0000000 5 4.400000