R - Create monthly mean by time intervals of multiple observations

ぃ、小莉子 提交于 2020-01-16 01:07:14

问题


Basically, this question is an extension of this one: Create monthly mean by time intervals

Now I have temperature observations from five different sources, and I would like to apply the same logic on all of them: calculate a data interval average for each of the columns (except for the last, which should be kept untouched).

First, what I need to do is to average all januaries, februaries, marches and etc for the whole period.

Then, I would like to do the same for specific periods of time (3 years, 5 years, 10 years etc).

For example,

  • The average of all jan, feb, mar etc between 1960 and 1964;
  • The average of all jan, feb, mar etc between 1965 and 1969;
  • and so on.

The final result would consist of month, period and each measurement of temperature, or something like this:

month   period   CanESM2  GFDL-ESM2M inmcm4 MRI-CGCM3 NorESM1-M scenario
jan   1981-1983     12        14        17      16        19      rcp45
feb   1981-1983     14        15        19      18        21      rcp45
mar   1981-1983     16        17        20      19        22      rcp45

and so on.

For the sake of this new example, the period intervals can be every two years. Please take a look at this new data frame:

df <- structure(list(date = structure(c(4018, 4049, 4077, 4108, 4138, 
4169, 4199, 4230, 4261, 4291, 4322, 4352, 4383, 4414, 4442, 4473, 
4503, 4534, 4564, 4595, 4626, 4656, 4687, 4717, 4748, 4779, 4807, 
4838, 4868, 4899, 4929, 4960, 4991, 5021, 5052, 5082, 5113, 5144, 
5173, 5204, 5234, 5265, 5295, 5326, 5357, 5387, 5418, 5448, 5479, 
5510, 5538, 5569, 5599, 5630, 5660, 5691, 5722, 5752, 5783, 5813
), class = "Date"), 
CanESM2 = c(23.3447584995004, 22.7553194622661, 
22.9046171099641, 23.2348274851954, 20.7867914244186, 16.1089263516803, 
17.7123198841893, 20.6117388481318, 22.6172482024792, 25.1417677901512, 
25.3728623279305, 25.4938629061677, 26.737161702888, 25.6469673555951, 
25.2708307088808, 22.0761146323625, 21.7333505763564, 17.8658950384273, 
18.7538162497587, 23.7302586311518, 22.1195592658464, 25.1213110324948, 
25.555077863294, 24.1775791256927, 24.976090364678, 24.4222929533138, 
23.5313765503639, 21.7510275951652, 20.8868661259496, 16.8938505372336, 
18.5591840522234, 19.4153537306675, 22.3315702704496, 22.079217600268, 
23.9921479779621, 24.1689841248268, 22.4950307358143, 23.3267362284106, 
21.7253085735232, 20.9087896790615, 21.2222563943198, 18.6345168712527, 
17.6959707127061, 19.2921804827313, 22.2201911793199, 21.3917114568311, 
24.5653552343679, 23.9938199021095, 24.1991599548695, 23.2070280563, 
22.6735137451527, 21.5380451291106, 20.9366566635842, 17.4557163881701, 
16.451680105786, 19.6839144950689, 22.9428780134334, 28.5044708917307, 
28.7486652995265, 25.601554427036), `GFDL-ESM2M` = c(22.7473744015361, 
23.267293708269, 22.6634474687798, 21.4769028951955, 16.8500707094059, 
14.0588449212008, 16.3309941291809, 20.8032760841902, 23.7906252838844, 
26.3377195180849, 23.9737603608952, 22.8020373721455, 23.6384940701862, 
23.2450169408044, 23.0613696963288, 20.3505857378937, 17.6190373398537, 
17.120993813803, 17.4628583109656, 21.7968657959339, 25.408205564632, 
27.1743670175242, 27.0112403603487, 24.1517303378083, 28.6996475707653, 
26.364532182383, 22.6005774653235, 20.6629485640415, 19.3802376458811, 
19.7960457690926, 18.0885937047559, 20.0774719326995, 24.4475449850393, 
29.0717597340429, 27.9275227036587, 28.6423556527426, 27.831002723339, 
27.5789989205294, 23.7086642065714, 22.5027575160182, 20.1774646182393, 
15.6902192137962, 18.8325861332028, 19.6784317992454, 22.0770099107609, 
23.1502915094065, 22.8506410288256, 24.2689494864885, 22.5562992539517, 
23.2636392726455, 23.0880691173465, 21.651142763537, 17.1273323990578, 
14.2451504552087, 17.2530248775039, 18.4977167706157, 24.5455986067306, 
24.7018910563269, 22.8956140695616, 22.3739924763524), 
inmcm4 = c(22.5132853264032, 
21.9670312349186, 21.518670725268, 19.6595980622048, 15.6128614115161, 
11.8782840994901, 12.9922682518183, 16.9240378668142, 19.6257835654325, 
24.7561076408209, 21.1491253875023, 25.739956811417, 22.777010939842, 
23.3323680301045, 21.7209638551224, 19.4641772203667, 14.8426936615345, 
14.492025508437, 13.4382055082987, 16.7792235307915, 20.9078206350637, 
22.3465580607569, 23.4736963316452, 26.3783092942349, 23.8321943504866, 
24.1664866070415, 21.0995867086011, 21.7002121681391, 16.4775339170944, 
13.5096931568412, 12.0038825966591, 14.984557928041, 21.1930935438289, 
24.2988129771033, 21.475747463315, 24.1769515414571, 22.0022813885711, 
22.070100983908, 21.1693579429804, 20.7176937280699, 15.2514348806337, 
14.5735194405844, 15.213183536086, 16.4017604339954, 19.8834069274193, 
24.1246425273807, 23.5816601376201, 22.7429871670035, 22.9034691744072, 
22.4474015790363, 21.9001880468324, 18.7982453856357, 14.5709439432898, 
12.6522844891215, 12.9150917585506, 16.4433334705442, 22.1611542590829, 
22.8982093722321, 20.5497707100802, 22.4826400002768), 
`MRI-CGCM3` = c(23.9085492644199, 
22.7815332190935, 22.9578186523083, 22.0595994993698, 20.1337316646132, 
18.8364091252172, 15.6515341803085, 18.7287123480508, 22.6648702177891, 
24.4228579055431, 23.7383908781894, 25.2184654280197, 23.9784546120222, 
23.832084433977, 24.2370294970135, 22.8301985319271, 18.1513480252998, 
17.1278084488802, 19.7040101871934, 19.8287493240002, 24.2791988905086, 
24.3074491412141, 24.9924176460089, 22.1821786525638, 24.060079907262, 
22.7810582227485, 23.980250003726, 23.0564833574517, 18.5242467481037, 
16.0317880608315, 17.5689669209857, 17.6411031678666, 21.7831934995429, 
21.9361926234046, 22.8829472342203, 22.945155809092, 23.2966231412666, 
24.1177874720374, 24.3898735046387, 22.8699703881907, 19.9047111910443, 
16.1351986042289, 17.5883813237035, 18.4408368177192, 25.4256881004156, 
26.2231168081594, 25.3052046576212, 23.6207903928535, 23.5182004307592, 
23.8721417050029, 24.7242025331009, 22.5079866453659, 20.5091285483782, 
15.7315025107805, 17.2953255010206, 19.3573555613673, 20.7117094882699, 
25.3324051568674, 24.681446008904, 23.1961686555729), 
`NorESM1-M` = c(23.2576241825902, 
26.4987746393958, 24.4529344869214, 23.2587863123694, 19.3106194873189, 
17.2435529731041, 17.2933378441389, 20.2791416256927, 24.1192653789077, 
22.6220495090928, 24.1891885580019, 25.3589668052141, 23.0063114831614, 
24.8025680808134, 24.2534722616506, 21.2083193978598, 18.5661563873291, 
16.2316653340362, 17.5604114754255, 18.3089694533237, 20.8568863536036, 
24.7456508902616, 22.9064842268478, 23.0127289128858, 23.9962792951007, 
23.6816674165947, 24.1090585353763, 21.7090812062108, 19.2465560380803, 
16.9848449507425, 17.3327291289041, 18.1265287510184, 24.3011344643526, 
25.7990417480469, 26.0647831406704, 24.093305609947, 24.1464228740958, 
25.1438548731249, 24.467590509459, 21.9169314850208, 19.30861872296, 
16.6311675337858, 17.1707304466602, 19.3059608548187, 23.5714481486831, 
25.3312428053035, 23.5855715330257, 23.8135553404342, 23.1161932169005, 
23.6003849561824, 23.6421707951745, 21.8508808002915, 20.276045688363, 
17.6758961788444, 18.459291369416, 19.4111283102701, 22.4235624934352, 
22.8147386062977, 25.3883792744126, 24.2199711910514), scenario = c("rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45")), .Names = c("date", "CanESM2", "GFDL-ESM2M", 
"inmcm4", "MRI-CGCM3", "NorESM1-M", "scenario"), row.names = c(NA, 
60L), class = "data.frame")

How can I achieve this? Many thanks for the help.


回答1:


Use the cut.Date and format.Date functions. Followed by the aggregate function once you have the correct grouping variables.

df$yr.grp <- cut(df$date, breaks="2 year", labels=c("81-82","83-84","85-86"))
df$mon.grp<- format(df$date,"%b")
aggregate(df[-c(1,7:9)], by=df[c(9,8,7)], FUN=mean)


来源:https://stackoverflow.com/questions/33472328/r-create-monthly-mean-by-time-intervals-of-multiple-observations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!