2

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

bioinformatician
  • 364
  • 1
  • 12
  • 27

2 Answers2

3

1) zoo has a fortify.zoo method which produces a data frame with an Index column so suppose r is the output of the rollapply given in the question. Then for a width of 3 the end dates are 2 days past the corresponding start dates so:

library(ggplot2)
r <- rollapply(z, width=3, by=2, mean, align="left") # as in question

DF <- transform(fortify(r), end_date = Index + 2)

giving:

> DF
       Index        r   end_date
1 1970-02-01 12.00000 1970-02-03
2 1970-02-03 14.00000 1970-02-05
3 1970-02-05 12.66667 1970-02-07
4 1970-02-07 13.00000 1970-02-09
5 1970-02-09 13.33333 1970-02-11
6 1970-02-11 12.00000 1970-02-13
7 1970-02-13 14.00000 1970-02-15

If the column order and column names must be as shown then:

DF <- setNames(DF[c(1, 3:2)], c("start_date", "end_date", "mean"))

2) Assuming r from above, this would also work:

data.frame(start_date = time(r), end_date = time(r) + 2, mean = coredata(r))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
2

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
puslet88
  • 1,288
  • 15
  • 25