Aggregate daily level data to weekly level in R
date, r, xts, zoo
Solution
If you mean the sum of of ‘value’ by week I think the easiest way to do it is to convert the data into a xts object as GSee suggested:
data <- as.xts(data$value,order.by=as.Date(data$interval))
weekly <- apply.weekly(data,sum)
[,1]
2012-06-10 552
2012-06-17 23629
2012-06-24 23872
2012-07-01 23667
2012-07-08 23552
2012-07-10 10902
I leave the formatting of the output as an exercise for you :-)
Problem
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 2012-06-13 4132 5 2012-06-14 4190 6 2012-06-15 4186 7 2012-06-16 1139 8 2012-06-17 490 9 2012-06-18 5156 10 2012-06-19 4430 11 2012-06-20 4447 12 2012-06-21 4256 13 2012-06-22 3856 14 2012-06-23 1163 15 2012-06-24 564 16 2012-06-25 4866 17 2012-06-26 4421 18 2012-06-27 4206 19 2012-06-28 4272 20 2012-06-29 3993 21 2012-06-30 1211 22 2012-07-01 698 23 2012-07-02 5770 24 2012-07-03 5103 25 2012-07-04 775 26 2012-07-05 5140 27 2012-07-06 4868 28 2012-07-07 1225 29 2012-07-08 671 30 2012-07-09 5726 31 2012-07-10 5176 ``` I want to aggregate this data to weekly level to get the output similar to the following: ``` Interval value 1 Week 2, June 2012 *aggregate value for day 10 to day 14 of June 2012* 2 Week 3, June 2012 *aggregate value for day 15 to day 21 of June 2012* 3 Week 4, June 2012 *aggregate value for day 22 to day 28 of June 2012* 4 Week 5, June 2012 *aggregate value for day 29 to day 30 of June 2012* 5 Week 1, July 2012 *aggregate value for day 1 to day 7 of July 2012* 6 Week 2, July 2012 *aggregate value for day 8 to day 10 of July 2012* ``` How do I achieve this easily without writing a long code?