首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每组汇总/汇总多个变量(例如sum、mean)

每组汇总/汇总多个变量(例如sum、mean)
EN

Stack Overflow用户
提问于 2012-03-15 23:44:55
回答 8查看 206.5K关注 0票数 172

从数据帧中,有没有一种简单的方法来同时聚合(summeanmax等c)多个变量?

以下是一些示例数据:

代码语言:javascript
复制
library(lubridate)
days = 365*2
date = seq(as.Date("2000-01-01"), length = days, by = "day")
year = year(date)
month = month(date)
x1 = cumsum(rnorm(days, 0.05)) 
x2 = cumsum(rnorm(days, 0.05))
df1 = data.frame(date, year, month, x1, x2)

我想按年和月同时聚合来自df2数据框的x1x2变量。下面的代码聚合了x1变量,但是同时聚合x2变量也是可能的吗?

代码语言:javascript
复制
### aggregate variables by year month
df2=aggregate(x1 ~ year+month, data=df1, sum, na.rm=TRUE)
head(df2)
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2012-03-15 23:56:54

这个year()函数来自哪里?

您还可以使用reshape2包来执行此任务:

代码语言:javascript
复制
require(reshape2)
df_melt <- melt(df1, id = c("date", "year", "month"))
dcast(df_melt, year + month ~ variable, sum)
#  year month         x1           x2
1  2000     1  -80.83405 -224.9540159
2  2000     2 -223.76331 -288.2418017
3  2000     3 -188.83930 -481.5601913
4  2000     4 -197.47797 -473.7137420
5  2000     5 -259.07928 -372.4563522
票数 47
EN

Stack Overflow用户

发布于 2012-03-15 23:50:02

可以,在您的formula中,您可以cbind要聚合的数值变量:

代码语言:javascript
复制
aggregate(cbind(x1, x2) ~ year + month, data = df1, sum, na.rm = TRUE)
   year month         x1          x2
1  2000     1   7.862002   -7.469298
2  2001     1 276.758209  474.384252
3  2000     2  13.122369 -128.122613
...
23 2000    12  63.436507  449.794454
24 2001    12 999.472226  922.726589

请参阅?aggregateformula参数和示例。

票数 197
EN

Stack Overflow用户

发布于 2015-10-16 18:19:13

使用dplyr包,您可以使用summarise_allsummarise_atsummarise_if函数同时聚合多个变量。对于示例数据集,您可以执行以下操作:

代码语言:javascript
复制
library(dplyr)
# summarising all non-grouping variables
df2 <- df1 %>% group_by(year, month) %>% summarise_all(sum)

# summarising a specific set of non-grouping variables
df2 <- df1 %>% group_by(year, month) %>% summarise_at(vars(x1, x2), sum)
df2 <- df1 %>% group_by(year, month) %>% summarise_at(vars(-date), sum)

# summarising a specific set of non-grouping variables using select_helpers
# see ?select_helpers for more options
df2 <- df1 %>% group_by(year, month) %>% summarise_at(vars(starts_with('x')), sum)
df2 <- df1 %>% group_by(year, month) %>% summarise_at(vars(matches('.*[0-9]')), sum)

# summarising a specific set of non-grouping variables based on condition (class)
df2 <- df1 %>% group_by(year, month) %>% summarise_if(is.numeric, sum)

后两个选项的结果:

代码语言:javascript
复制
    year month        x1         x2
   <dbl> <dbl>     <dbl>      <dbl>
1   2000     1 -73.58134  -92.78595
2   2000     2 -57.81334 -152.36983
3   2000     3 122.68758  153.55243
4   2000     4 450.24980  285.56374
5   2000     5 678.37867  384.42888
6   2000     6 792.68696  530.28694
7   2000     7 908.58795  452.31222
8   2000     8 710.69928  719.35225
9   2000     9 725.06079  914.93687
10  2000    10 770.60304  863.39337
# ... with 14 more rows

注意:为了支持summarise_allsummarise_atsummarise_ifsummarise_each被弃用。

正如在my comment above中提到的,您还可以使用reshape2-package中的recast函数:

代码语言:javascript
复制
library(reshape2)
recast(df1, year + month ~ variable, sum, id.var = c("date", "year", "month"))

这将会给你同样的结果。

票数 54
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9723208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档