包括年度值作为时间序列的月度平均值

时间:2017-05-23 16:53:28

标签: r

我每年都有关于二氧化碳生产的数据。目前没有可用的数据,可以追溯到1880年。我希望将年平均值(比如1881年)作为每年1月到12月之间每月的月平均值。我怎么能在R中这样做?

1 个答案:

答案 0 :(得分:0)

假设您有两个看起来像

的data.frames
co2month <- data.frame(year=rep(1900:1902,each=12), month=rep(month.abb,3), co2=rnorm(36))
co2month
#    year month         co2
# 1  1900   Jan  1.76168513
# 2  1900   Feb -1.62766625
# 3  1900   Mar  0.17191233
# 4  1900   Apr -1.86788375
# 5  1900   May -1.91116179
# 6  1900   Jun  1.52547400
# 7  1900   Jul  0.55842987
# 8  1900   Aug -0.02160731
# 9  1900   Sep -0.22032746
# 10 1900   Oct -1.00799922
# 11 1900   Nov  0.48254329
# 12 1900   Dec  0.65806450
# 13 1901   Jan  0.34173786
# 14 1901   Feb  0.24271387
# 15 1901   Mar  0.10781086
# 16 1901   Apr  0.84838652
# 17 1901   May -0.33528728
# 18 1901   Jun  1.08970247
# 19 1901   Jul  0.54564873
# 20 1901   Aug -0.08503711
# 21 1901   Sep -0.64936354
# 22 1901   Oct -0.38287041
# 23 1901   Nov -1.31713823
# 24 1901   Dec  0.35556969
# 25 1902   Jan -0.84373047
# 26 1902   Feb -0.53323007
# 27 1902   Mar  0.30167420
# 28 1902   Apr  0.64759628
# 29 1902   May -0.15417477
# 30 1902   Jun  0.77399708
# 31 1902   Jul  0.07909732
# 32 1902   Aug -1.81445087
# 33 1902   Sep -0.14126574
# 34 1902   Oct -0.37610929
# 35 1902   Nov -1.15516717
# 36 1902   Dec  0.77210347

co2year <- data.frame(year=1897:1899, co2=rnorm(3))
co2year
#   year         co2
# 1 1897 -0.75838749
# 2 1898 -1.61749706
# 3 1899 -0.04063466

你可以扩展早期的'co2year}数据并附加两个数据集

# expanding
co2month1 <- data.frame(year=rep(co2year$year,each=12), month=rep(month.abb,3), co2=rep(co2year$co2,each=12))

# appending
co2month_appended <- rbind(co2month1, co2month)

# results
co2month_appended
#    year month         co2
# 1  1897   Jan -0.75838749
# 2  1897   Feb -0.75838749
# 3  1897   Mar -0.75838749
# 4  1897   Apr -0.75838749
# 5  1897   May -0.75838749
# 6  1897   Jun -0.75838749
# 7  1897   Jul -0.75838749
# 8  1897   Aug -0.75838749
# 9  1897   Sep -0.75838749
# 10 1897   Oct -0.75838749
# 11 1897   Nov -0.75838749
# 12 1897   Dec -0.75838749
# 13 1898   Jan -1.61749706
# 14 1898   Feb -1.61749706
# 15 1898   Mar -1.61749706
# 16 1898   Apr -1.61749706
# 17 1898   May -1.61749706
# 18 1898   Jun -1.61749706
# 19 1898   Jul -1.61749706
# 20 1898   Aug -1.61749706
# 21 1898   Sep -1.61749706
# 22 1898   Oct -1.61749706
# 23 1898   Nov -1.61749706
# 24 1898   Dec -1.61749706
# 25 1899   Jan -0.04063466
# 26 1899   Feb -0.04063466
# 27 1899   Mar -0.04063466
# 28 1899   Apr -0.04063466
# 29 1899   May -0.04063466
# 30 1899   Jun -0.04063466
# 31 1899   Jul -0.04063466
# 32 1899   Aug -0.04063466
# 33 1899   Sep -0.04063466
# 34 1899   Oct -0.04063466
# 35 1899   Nov -0.04063466
# 36 1899   Dec -0.04063466
# 37 1900   Jan  1.76168513
# 38 1900   Feb -1.62766625
# 39 1900   Mar  0.17191233
# 40 1900   Apr -1.86788375
# 41 1900   May -1.91116179
# 42 1900   Jun  1.52547400
# 43 1900   Jul  0.55842987
# 44 1900   Aug -0.02160731
# 45 1900   Sep -0.22032746
# 46 1900   Oct -1.00799922
# 47 1900   Nov  0.48254329
# 48 1900   Dec  0.65806450
# 49 1901   Jan  0.34173786
# 50 1901   Feb  0.24271387
# 51 1901   Mar  0.10781086
# ... and so on ...

至于是否适当来估算年度平均值,我会留给你(主题专家)来决定。