每月数据的季节性汇总

每月数据的季节性汇总

本文介绍了每月数据的季节性汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框 df ,其中包含x,y和month.year数据每个x,y点.
我正在尝试获取季节性总计.我需要计算季节性平均值,即
冬季平均值(12月,1月,2月);春季平均值(3月,4月,5月),夏季平均值(6月,7月,8月)和秋季平均值(9月,10月,11月).

I have dataframe df with x,y,and monthly.year data for each x,y point.
I am trying to get the seasonal aggregate.I need to calculate seasonal means i.e.
For winter mean of (December,January,February); for Spring mean of (March,April,May), for Summer mean of (June,July,August) and for autumn mean of (September,October,November).

数据看起来类似于:

set.seed(1)
df <- data.frame(x=1:3,y=1:3, matrix(rnorm(72),nrow=3) )
names(df)[3:26] <- paste(month.abb,rep(2009:2010,each=12),sep=".")

  x y   Jan.2009   Feb.2009 ...   Dec.2010
1 1 1 -0.6264538  1.5952808 ...  2.1726117
2 2 2  0.1836433  0.3295078 ...  0.4755095
3 3 3 -0.8356286 -0.8204684 ... -0.7099464

除了融化数据并制作新数据框为

I could not think of going any further except melting the data and making new data frame as

ddt.m<-melt(df,id=c("x","y"))

我想要每年的x,y,mean之类的结果.请建议我我怎么能做到这一点.

I want result like x,y,mean of season of each year.Please suggest me how I may be able to do that.

推荐答案

以下是一种可能的方法:

Here is one possible approach:

...,然后使用colsplit将变量"分为星期一"和年份"列.

...and, use colsplit to split up the "variable" into the "Mon" and "Year" columns.

library(reshape2)
ddt.m <- melt(df, id = c("x", "y"))
ddt.m <- cbind(ddt.m, colsplit(ddt.m$variable, "\\.", c("Mon", "Year")))

使用factorlevels来获取您的季节

(我留在星期一"列中.糟糕!)

Use factor and levels to get your seasons

(which I've left in the "Mon" column. Oops.)

ddt.m$Mon <- factor(ddt.m$Mon)
levels(ddt.m$Mon) <- list(Winter = month.abb[c(12, 1, 2)],
                          Spring = month.abb[c(3:5)],
                          Summer = month.abb[c(6:8)],
                          Autumn = month.abb[c(9:11)])
head(ddt.m)
#         x        y variable     value    Mon Year
# 1 1214842 991964.4 Jan.2009 -1.332933 Winter 2009
# 2 1220442 991964.4 Jan.2009 -1.345808 Winter 2009
# 3 1226042 991964.4 Jan.2009 -1.314435 Winter 2009
# 4 1231642 991964.4 Jan.2009 -1.236600 Winter 2009
# 5 1237242 991964.4 Jan.2009 -1.261989 Winter 2009
# 6 1242842 991964.4 Jan.2009 -1.306614 Winter 2009

使用dcast汇总数据

Use dcast to aggregate the data

dfSeasonMean <- dcast(ddt.m, x + y ~ Mon + Year,
                      value.var="value", fun.aggregate=mean)
head(dfSeasonMean)
#         x        y Winter_2009 Winter_2010 Spring_2009 Spring_2010 Summer_2009
# 1 1214842 991964.4   -1.439480   -1.006512 -0.02509008   0.2823048    1.392440
# 2 1220442 964154.4   -1.457407   -1.039266 -0.04337596   0.2315217    1.422541
# 3 1220442 973424.4   -1.456991   -1.035115 -0.04117584   0.2423561    1.414473
# 4 1220442 982694.4   -1.456479   -1.029627 -0.03799926   0.2544062    1.405813
# 5 1220442 991964.4   -1.456234   -1.027081 -0.03815661   0.2610397    1.400743
# 6 1226042 945614.4   -1.463465   -1.031665 -0.04288670   0.2236609    1.434002
#   Summer_2010 Autumn_2009 Autumn_2010
# 1    1.256840  0.06469363 -0.03823892
# 2    1.263593  0.04521096 -0.04485553
# 3    1.258328  0.04860321 -0.04477636
# 4    1.252779  0.05337575 -0.04729598
# 5    1.247251  0.05742809 -0.05152524
# 6    1.272742  0.04692731 -0.04915314

这篇关于每月数据的季节性汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:51