I have an xts object with 900 columns (x1-x900) of daily values, that I need to compute into monthly returns.
x <- data.frame(date=seq(as.Date("2016/7/4"), as.Date("2016/10/1"), "day"),x1=runif(90,.95,1.07),x2=runif(90,.95,1.07),
x3=runif(90,.95,1.07),x4=runif(90,.95,1.07),x5=runif(90,.95,1.07),x6=runif(90,.95,1.07),x7=runif(90,.95,1.07))
Above is a sample of the data I am working with. What I need to do is get the product of the values for each month, and for each column, for 7/31/2016, 8/31/2016, 9/30/2016, etc. I do not want to use zoo::rollapply
because the length of each month obviously is not fixed. I have tried summarize, aggregate, but I haven't figured this out, and I am trying to avoid having to do a "for" loop.
The end goal is to obtain a data.frame
such as:
Date x1 x2 x3 x4
7/31/16 1.03 0.98 1.01 1.03
8/31/16 1.01 0.95 1.03 1.01
9/30/16 0.97 1.02 0.94 0.98
10/31/16 0.99 0.98 1.01 1.04