Calculate monthly average of ts object

后端 未结 1 1915
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 15:40

Given a monthly ts object such as this:

dat <- ts(c(295, 286, 300, 278, 272, 268, 308, 321, 313, 308, 291, 296, 
294, 273, 300, 271, 282, 285, 318, 323, 3         


        
相关标签:
1条回答
  • 2020-12-03 16:06

    Ok, so I should given Google one more search before coming to SO as this post is relevant. The cycle() function appears to be useful for these sorts of things:

    > tapply(dat, cycle(dat), mean)
            1         2         3         4         5         6         7         8         9 
    295.33333 277.33333 298.00000 269.33333 276.66667 282.33333 314.00000 323.00000 313.66667 
           10        11        12 
    310.33333 291.33333 296.66667
    
    > aggregate(c(dat), list(month = cycle(dat)), mean) 
       month         x
    1      1 295.33333
    2      2 277.33333
    3      3 298.00000
    ....
    

    Anything else fundamental I'm missing here?

    0 讨论(0)
提交回复
热议问题