Aggregate daily level data to weekly level in R

前端 未结 5 749
无人及你
无人及你 2021-01-03 16:55

I have a huge dataset similar to the following reproducible sample data.

   Interval    value
1  2012-06-10   552
2  2012-06-11  4850
3  2012-06-12  4642
4          


        
5条回答
  •  萌比男神i
    2021-01-03 17:36

    If you were to use week from lubridate, you would only get five weeks to pass to by. Assume dat is your data,

    > library(lubridate)
    > do.call(rbind, by(dat$value, week(dat$Interval), summary))
    #    Min. 1st Qu. Median Mean 3rd Qu. Max.
    # 24  552    4146   4188 3759    4529 4850
    # 25  490    2498   4256 3396    4438 5156
    # 26  564    2578   4206 3355    4346 4866
    # 27  698     993   4868 3366    5122 5770
    # 28  671    1086   3200 3200    5314 5726
    

    This shows a summary for the 24th through 28th week of the year. Similarly, we can get the means with aggregate with

    > aggregate(value~week(Interval), data = dat, mean)
    #   week(Interval)    value
    # 1             24 3758.667
    # 2             25 3396.286
    # 3             26 3355.000
    # 4             27 3366.429
    # 5             28 3199.500
    

提交回复
热议问题