A dummy zoo object is created as
z <- zoo(11:15, as.Date(31:45))
as.data.frame(z)
z
1970-02-01 11
1970-02-02 12
1970-02-03 13
1970-02-04 14
1970-02-05 15
1970-02-06 11
1970-02-07 12
1970-02-08 13
1970-02-09 14
1970-02-10 15
1970-02-11 11
1970-02-12 12
1970-02-13 13
1970-02-14 14
1970-02-15 15
rollapply function can be used to calculate mean as:
as.data.frame(rollapply(z, width=3, by=2, mean, align="left"))
1970-02-01 12.00000
1970-02-03 14.00000
1970-02-05 12.66667
1970-02-07 13.00000
1970-02-09 13.33333
1970-02-11 12.00000
1970-02-13 14.00000
Format which I want : Is it possible to add another column (II column/ end window) having end date as shown below [using rollapply or some other method using xts/zoo object as used above]
start_window end_window mean
1970-02-01 1970-02-03 12.00000
1970-02-03 1970-02-05 14.00000
1970-02-05 1970-02-07 12.66667
1970-02-07 1970-02-09 13.00000
1970-02-09 1970-02-11 13.33333
1970-02-11 1970-02-13 12.00000
1970-02-13 1970-02-15 14.00000
Please suggest a way to do so. Thanks in advance
You can make a simple hack by just adding the results of two rollapply-s into a dataframe.
#Your code
library(zoo)
z <- zoo(11:15, as.Date(31:45))
as.data.frame(z)
as.data.frame(rollapply(z, width=3, by=2, mean, align="left"))
Data for start and end of the reference
frame1 <- as.data.frame(rollapply(z, width=3, by=2, mean, align="left"))
frame2 <- as.data.frame(rollapply(z, width=3, by=2, mean, align="right"))
Add them to a data frame
frame3 <- data.frame(Start = row.names(frame1), Finish = row.names(frame2), frame1[1])
row.names(frame3) <- c(1:length(frame3[,1]))
names(frame3)[3] <- "Mean"
Result
frame3
Start Finish Mean
1 1970-02-01 1970-02-03 12.00000
2 1970-02-03 1970-02-05 14.00000
3 1970-02-05 1970-02-07 12.66667
4 1970-02-07 1970-02-09 13.00000
5 1970-02-09 1970-02-11 13.33333
6 1970-02-11 1970-02-13 12.00000
7 1970-02-13 1970-02-15 14.00000