I am trying to use the apply.daily/weekly/monthly functions with xts in R, but I need to have the apply function work on subsets at a time. For example,
x=xts(data.frame(value=1:100,code=rep(1:5,20)),
seq(as.Date('2011-01-01'),by=1,length.out=100))
Step 1: I'd like to roll-up by week AND "code", so I'd have something like
ROW 1: Week = Week 1, Code = 1, Sum(all entries that have a code of 1 that fall in week 1)
ROW 2: Week = Week 1, Code = 2, Sum(all entries that have a code of 2 that fall in week 1)
...
ROW 70: Week = Week 10, Code = 1, Sum(all entries that have a code of 1 that fall in week 10)
Step 2: I'd like the same number of rows for each week, because ultimately I want a matrix--one row per code and one column per week. I'd prefer not to create separate week variables as the first answer suggests because I'm going to need to cut this again by month, day, hour, minute, and maybe even custom time duration. I'm happy to bypass the first step, because that's only an intermediate output.
Unfortunately in my real data I can't subset manually because I have 10,000+ "codes" and 53M rows.