在R中按特定年份汇总 [英] Aggregate by specific year in R

查看:175
本文介绍了在R中按特定年份汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这个问题已经在SO上解决了,但是到目前为止我似乎还没有找到一个快速的解决方案。

Apologies if this question has already been dealt with already on SO, but I cannot seem to find a quick solution as of yet.

我正在尝试汇总一个特定年份的数据集。我的数据框包含10年内的每小时气候数据。

I am trying to aggregate a dataset by a specific year. My data frame consists of hourly climate data over a period of 10 years.

head(df)
#  day month year hour rain temp pressure wind
#1   1     1 2005    0    0  7.6     1016   15
#2   1     1 2005    1    0  8.0     1015   14
#3   1     1 2005    2    0  7.7     1014   15
#4   1     1 2005    3    0  7.8     1013   17
#5   1     1 2005    4    0  7.3     1012   17
#6   1     1 2005    5    0  7.6     1010   17

要从上述数据集中计算每日均值,请使用此聚合函数

To calculate daily means from the above dataset, I use this aggregate function

g <- aggregate(cbind(temp,pressure,wind) ~ day + month + year, d, mean)
options(digits=2)

head(g)
#  day month year temp pressure wind
#1   1     1 2005  6.6     1005   25
#2   2     1 2005  6.5     1018   25
#3   3     1 2005  9.7     1019   22
#4   4     1 2005  7.5     1010   25
#5   5     1 2005  7.3     1008   25
#6   6     1 2005  9.6     1009   26

不幸的是,我得到了一个庞大的数据集,涵盖了整个10年(2005年至2014年)。我想知道是否有人可以帮助我调整上面的汇总代码,这样我就可以汇总特定年份的每日均值,而不是一次滑动所有均值?

Unfortunately, I get a huge dataset spanning the whole 10 years (2005 to 2014). I am wondering if anybody would be able to help me tweak the above aggregate code so as I would be able to summaries daily means over a specific year as opposed to all of them in one swipe?

推荐答案

您可以在聚合 <中使用子集自变量/ p>

You can use the subset argument in aggregate

aggregate(cbind(temp,pressure,wind) ~ day + month + year, df, 
                     subset=year %in% 2005:2014, mean)

这篇关于在R中按特定年份汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆