简体   繁体   English

来自月度数据的时间序列的日均值 R monthdays()

[英]Daily Average of Time series derived from monthly data R monthdays()

I have a time series object ts.我有一个时间序列 object ts。 I have mentioned the entire object here.我在这里提到了整个 object。 It has data from Jan 2013 to Dec 2017 for all years.它拥有 2013 年 1 月至 2017 年 12 月的所有年份数据。 I am trying to find the daily average value so that the value is divided by the number of days in a month.我试图找到每日平均值,以便将该值除以一个月中的天数。

Expected output预期 output

The first value for Jan 2013 in ts is 23770, I want the value to be 23770/31 where 31 is the number of days in Jan, second value for Feb 2013 is 23482. I want the value to be 23482/28 as 28 was the number of days in Feb 2013 and so on ts 中 2013 年 1 月的第一个值为 23770,我希望值为 23770/31,其中 31 是 1 月的天数,2013 年 2 月的第二个值为 23482。我希望值为 23482/28,因为 28 是2013 年 2 月的天数等

Tried so far:到目前为止尝试过:

I know monthdays() can do this.我知道monthdays()可以做到这一点。 Something like ts/monthdays() .Monthdays() returns number of days in a month.类似ts/monthdays() .Monthdays() 的东西返回一个月的天数。 I am not able to implement it here.我无法在这里实现它。 Read about this tapply somewhere but it is not giving me desired result, since i need values corresponding to each month year combination.在某处阅读这个tapply,但它没有给我想要的结果,因为我需要对应于每个月年组合的值。

    ts
    Jan   Feb   Mar   Apr   May   Jun   Jul   Aug    Sep   Oct   Nov   Dec
2013 23770 23482 23601 22889 23401 24240 23873 23647  23378 23871 22624 23496
2014 26765 27619 26341 27320 27389 27418 26874 27005  27538 26324 27267 27583
2015 28354 27452 28336 28998 28595 28338 27806 28660  27226 28317 28666 28574
2016 30209 30659 31554 30248 30358 31091 30389 30247 31227 31839 30602 30609
2017 32180 32203 31639 31784 32375 30856 31863 32827 32506 31702 31681 32176

 > cycle(ts_actual_group2)
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013   1   2   3   4   5   6   7   8   9  10  11  12
2014   1   2   3   4   5   6   7   8   9  10  11  12
2015   1   2   3   4   5   6   7   8   9  10  11  12
2016   1   2   3   4   5   6   7   8   9  10  11  12
2017   1   2   3   4   5   6   7   8   9  10  11  12

Using tapply since i read it, but this is not giving desired output自从我阅读以来就使用了tapply,但这并没有给出所需的 output

tapply(ts_actual_group2, cycle(ts_actual_group2), mean)
      1       2       3       4       5       6       7    8       9      10      11      12
28255.6 28283.0 28294.2 28247.8 28423.6 28388.6 28161.0 28477.2 28375.0 28410.6 28168.0 28487.6 

I am not able to implement it here.我无法在这里实现它。

I'm not sure why you couldn't.我不知道你为什么不能。 The monthdays function from the forecast package, when applied to a ts object, returns the number of days in each month of the series.当应用于ts monthdays时,来自预测package 的月份 function 返回系列中每个月的天数。 The object returned is a time-series of the same dimension as the input.返回的 object 是与输入维度相同的时间序列。 So you can simply divide them.所以你可以简单地划分它们。

library(forecast)

ts/monthdays(ts)

Jan       Feb       Mar       Apr       May       Jun       Jul 
2013  766.7742  838.6429  761.3226  762.9667  754.8710  808.0000
2014  863.3871  986.3929  849.7097  910.6667  883.5161  913.9333
2015  914.6452  980.4286  914.0645  966.6000  922.4194  944.6000
2016  974.4839 1057.2069 1017.8710 1008.2667  979.2903 1036.3667
2017 1038.0645 1150.1071 1020.6129 1059.4667 1044.3548 1028.5333

monthsdays(ts)  # Accepts a time-series object
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013  31  28  31  30  31  30  31  31  30  31  30  31
2014  31  28  31  30  31  30  31  31  30  31  30  31
2015  31  28  31  30  31  30  31  31  30  31  30  31
2016  31  29  31  30  31  30  31  31  30  31  30  31
2017  31  28  31  30  31  30  31  31  30  31  30  31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM