前言

这个函数的功能比较强大,它首先将数据进行分组(按行),然后对每一组数据进行函数统计,最后把结果组合成一个比较nice的表格返回。根据数据对象不同它有三种用法,分别应用于数据框(data.frame)、公式(formula)和时间序列(ts):

aggregate(x, by, FUN, ..., simplify =TRUE)

aggregate(formula, data, FUN, ..., subset, na.action=na.omit)

aggregate(x, nfrequency= 1, FUN = sum, ndeltat = 1, ts.eps = getOption("ts.eps"), ...)

语法

aggregate(x, ...)

## S3 method for class 'default':

aggregate((x, ...))

## S3 method for class 'data.frame':

aggregate((x, by, FUN, ..., simplify = TRUE))

## S3 method for class 'formula':

aggregate((formula, data, FUN, ...,

subset, na.action = na.omit))

## S3 method for class 'ts':

aggregate((x, nfrequency = 1, FUN = sum, ndeltat = 1,

ts.eps = getOption("ts.eps"), ...))

###细节查看 ?aggregate

Example1

我们通过 mtcars 数据集的操作对这个函数进行简单了解。mtcars 是不同类型汽车道路测试的数据框类型数据:

>str(mtcars)

'data.frame': 32 obs. of 11 variables:

$mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...

$cyl : num 6 6 4 6 8 6 8 4 4 6 ...

$disp: num 160 160 108 258 360 ...

$hp : num 110 110 93 110 175 105 245 62 95 123 ...

$drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...

$wt : num 2.62 2.88 2.32 3.21 3.44 ...

$qsec: num 16.5 17 18.6 19.4 17 ...

$vs : num 0 0 1 1 0 1 0 1 1 1 ...

$am : num 1 1 1 0 0 0 0 0 0 0 ...

$gear: num 4 4 4 3 3 3 3 4 4 4 ...

$carb: num 4 4 1 1 2 1 4 2 2 4 ...

先用attach函数把mtcars的列变量名称加入到变量搜索范围内,然后使用aggregate函数按cyl(汽缸数)进行分类计算平均值:

>attach(mtcars)

>aggregate(mtcars, by=list(cyl), FUN=mean)

Group.1 mpg cyl disp hp drat wt qsec vs am gear carb

1 4 26.66364 4 105.1364 82.63636 4.070909 2.285727 19.13727 0.9090909 0.7272727 4.090909 1.545455

2 6 19.74286 6 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286 0.4285714 3.857143 3.428571

3 8 15.10000 8 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000 0.1428571 3.285714 3.500000

by参数也可以包含多个类型的因子,得到的就是每个不同因子组合的统计结果:

> aggregate(mtcars, by=list(cyl, gear), FUN=mean)

Group.1 Group.2 mpg cyl disp hp drat wt qsec vs am gear carb

1 4 3 21.500 4 120.1000 97.0000 3.700000 2.465000 20.0100 1.0 0.00 3 1.000000

2 6 3 19.750 6 241.5000 107.5000 2.920000 3.337500 19.8300 1.0 0.00 3 1.000000

3 8 3 15.050 8 357.6167 194.1667 3.120833 4.104083 17.1425 0.0 0.00 3 3.083333

4 4 4 26.925 4 102.6250 76.0000 4.110000 2.378125 19.6125 1.0 0.75 4 1.500000

5 6 4 19.750 6 163.8000 116.5000 3.910000 3.093750 17.6700 0.5 0.50 4 4.000000

6 4 5 28.200 4 107.7000 102.0000 4.100000 1.826500 16.8000 0.5 1.00 5 2.000000

7 6 5 19.700 6 145.0000 175.0000 3.620000 2.770000 15.5000 0.0 1.00 5 6.000000

8 8 5 15.400 8 326.0000 299.5000 3.880000 3.370000 14.5500 0.0 1.00 5 6.000000

公式(formula)是一种特殊的R数据对象,在aggregate函数中使用公式参数可以对数据框的部分指标进行统计:

>aggregate(cbind(mpg,hp) ~ cyl+gear, FUN=mean)

cyl gear mpg hp

1 4 3 21.500 97.0000

2 6 3 19.750 107.5000

3 8 3 15.050 194.1667

4 4 4 26.925 76.0000

5 6 4 19.750 116.5000

6 4 5 28.200 102.0000

7 6 5 19.700 175.0000

8 8 5 15.400 299.5000

上面的公式 cbind(mpg,hp) ~ cyl+gear 表示使用 cyl 和 gear 的因子组合对 cbind(mpg,hp) 数据进行操作。aggregate在时间序列数据上的应用请参考R的函数说明文档。

Example2

## Compute the averages for the variables in 'state.x77', grouped

## according to the region (Northeast, South, North Central, West) that

## each state belongs to.

aggregate(state.x77, list(Region = state.region), mean)

## Compute the averages according to region and the occurrence of more

## than 130 days of frost.

aggregate(state.x77,

list(Region = state.region,

Cold = state.x77[,"Frost"] > 130),

mean)

## (Note that no state in 'South' is THAT cold.)

## example with character variables and NAs

testDF

v2 = c(11,33,55,77,88,33,55,NA,44,55,77,99) )

by1

by2

aggregate(x = testDF, by = list(by1, by2), FUN = "mean")

#and if you want to treat NAs as a group

fby1

fby2

aggregate(x = testDF, by = list(fby1, fby2), FUN = "mean")

## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many:

aggregate(weight ~ feed, data = chickwts, mean)

aggregate(breaks ~ wool + tension, data = warpbreaks, mean)

aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean)

aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum)

## Dot notation:

aggregate(. ~ Species, data = iris, mean)

aggregate(len ~ ., data = ToothGrowth, mean)

## Often followed by xtabs():

ag

xtabs(len ~ ., data = ag)

## Compute the average annual approval ratings for American presidents.

aggregate(presidents, nfrequency = 1, FUN = mean)

## Give the summer less weight.

aggregate(presidents, nfrequency = 1,

FUN = weighted.mean, w = c(1, 1, 0.5, 1))

Example3

------------------------------------------------------

#load data

data

head(data)

weight Time Chick Diet

1 42 0 1 1

2 51 2 1 1

3 59 4 1 1

4 64 6 1 1

5 76 8 1 1

6 93 10 1 1

#dimension of the data

dim(data)

[1] 578 4

#how many chickens

unique(data$Chick)

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

[31] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

50 Levels: 18 < 16 < 15 < 13 < 9 < 20 < 10 < 8 < 17 < 19 < 4 < 6 < 11 < 3 < 1 < 12 < ... < 48

#how many diets

unique(data$Diet)

[1] 1 2 3 4

Levels: 1 2 3 4

#how many time points

unique(data$Time)

[1] 0 2 4 6 8 10 12 14 16 18 20 21

library(ggplot2)

ggplot(data=data, aes(x=Time, y=weight, group=Chick, colour=Chick)) +

geom_line() +

geom_point()

------------------------------------------------------

## S3 method for class 'data.frame'

## aggregate(x, by, FUN, ..., simplify = TRUE)

#find the mean weight depending on diet

aggregate(data$weight, list(diet = data$Diet), mean)

diet x

1 1 102.6455

2 2 122.6167

3 3 142.9500

4 4 135.2627

#aggregate on time

aggregate(data$weight, list(time=data$Time), mean)

time x

1 0 41.06000

2 2 49.22000

3 4 59.95918

4 6 74.30612

5 8 91.24490

6 10 107.83673

7 12 129.24490

8 14 143.81250

9 16 168.08511

10 18 190.19149

11 20 209.71739

12 21 218.68889

#use a different function

aggregate(data$weight, list(time=data$Time), sd)

time x

1 0 1.132272

2 2 3.688316

3 4 4.495179

4 6

r语言aggredate_R语言 aggregate函数相关推荐

  1. r语言aggredate_R语言 分组计算,不止group_by

    最近在研究excel透视图,想到好像自己在R-分组操作并不是很流畅,顺便学习分享一下.R自带数据集比较多,今天就选择一个我想对了解的mtcars数据集带大家学习一下R语言中的分组计算(操作). 目录 ...

  2. R语言按组聚合求和实战(sum a variable by group):使用aggregate函数按组聚合求和、使用tapply函数按组聚合求和、按组聚合求和(使用dplyr包)

    R语言按组聚合求和实战(sum a variable by group):使用aggregate函数按组聚合求和.使用tapply函数按组聚合求和.按组聚合求和(使用dplyr包) 目录

  3. R语言aggregate函数数据聚合实战

    R语言aggregate函数数据聚合实战 目录 R语言aggregate函数数据聚合实战 #基本语法 # aggregate函数计算分组的均值(mean)

  4. R语言aggregate函数

    aggregate函数可以按照要求把数据打组聚合,然后对聚合以后的数据进行加和.求平均等各种操作. 通过by设置分组因子,可以用多个类型的因子,可参见https://www.cnblogs.com/c ...

  5. R语言使用download.file函数下载网络文件到本地(Download File from the Internet)

    R语言使用download.file函数下载网络文件到本地(Download File from the Internet) 目录 R语言使用download.file函数下载网络文件到本地(Down ...

  6. R语言stringr包str_dup函数字符串多次复制实战

    R语言stringr包str_dup函数字符串多次复制实战 目录 R语言stringr包str_dup函数字符串多次复制实战 #导入stringr包 #仿真数据

  7. R语言stringr包str_count函数计算字符串匹配个数实战

    R语言stringr包str_count函数计算字符串匹配个数实战 目录 R语言stringr包str_count函数计算字符串匹配个数实战 #导入stringr包 #仿真数据

  8. R语言ggpubr包ggsummarystats函数可视化分组条形图(自定义分组颜色、添加抖动数据点jitter、误差条)并在X轴标签下方添加分组对应的统计值(样本数N、中位数、四分位数的间距iqr)

    R语言ggpubr包ggsummarystats函数可视化分组条形图(自定义分组颜色.添加抖动数据点jitter.误差条error bar)并在X轴标签下方添加分组对应的统计值(样本数N.中位数med ...

  9. R语言使用t.test函数计算两组独立数据的t检验(Independent t-test)

    R语言使用t.test函数计算两组独立数据的t检验(Independent t-test) 目录 R语言使用t.test函数计算两组独立数据的t检验(Independent t-test) #仿真数据

最新文章

  1. 把三千行代码重构为15行
  2. 敲代码、作诗、写论文无所不能!史上最大AI模型GPT-3强势霸榜Github
  3. docker 安装iproute包 包含 ss ip 命令
  4. Challenges(分组、聚合、连接)
  5. 带你遍历用户生命价值与流失挽救(上) : 流量下的价值套路
  6. 小甲鱼python的课后题好难_小甲鱼《零基础学习Python》课后笔记(二十六):字典——当索引不好用时2...
  7. 分析:windows下cmd默认的编码是ASCII编码 ,windows的中文环境下编码是GBK 方法一:在保存输出流保存的时候做一个对文字GBK编码,在输出到文件 如下 [python] view
  8. 首次曝光 | 阿里数万名开发者都在使用的数据库开发工具到底长什么样?
  9. Bella Email邮件发送模板
  10. java中数组合并的方法,数组合并--Java原生方法
  11. createSQLQuery与createQuery的区别
  12. 【Python】嵌套类的定义与使用
  13. [POJ1952]BUY LOW, BUY LOWER
  14. Linux忘记密码了怎么修改密码(保姆级教程)
  15. 2021美赛C题M奖思路
  16. 【DM】Combating Web Spam with TrustRank - 用TrustRank对抗网络垃圾邮件
  17. 苹果html 闪退,苹果打开软件闪退是怎么回事_苹果手机一打开软件就闪退如何解决-win7之家...
  18. java学习笔记参考
  19. 中央空调系统运行原理以及相关设备介绍
  20. 基于STM32指纹密码锁设计

热门文章

  1. 以pri为处理参数的雷达信号分选
  2. 配置 4G 模块为WAN口上网
  3. Linux中gcc编译步骤
  4. Matlab 创建文件夹
  5. Android官方架构组件Paging:分页库的设计美学
  6. Feign调用原理分析
  7. 软件更新服务之客户端更新
  8. Vim工具打开、编辑、保存文件
  9. 腾讯 SNG 监控数据的创新应用
  10. mysql全文检索优化_mysql全文检索 实现搜索优化