rollapply :是否可以为每个滑动添加结束日期 window?

rollapply : Is it possible to add end date for each sliding window?

虚拟动物园对象创建为

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

我想要的格式: 是否可以添加另一列(II 列/结束 window),其结束日期如下所示[使用 rollapply 或其他使用 xts/zoo 对象的方法]

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

请建议一种方法。提前致谢

1) zoo 有一个 fortify.zoo 方法生成一个带有 Index 列的数据框,所以假设 r是问题中给出的 rollapply 的输出。那么对于 3 的宽度,结束日期比相应的开始日期晚 2 天,所以:

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

如果列顺序和列名称必须如图所示:

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

2) 假设上面 r,这也可行:

data.frame(start_date = time(r), end_date = time(r) + 2, mean = coredata(r))

您只需将两个 rollapply-s 的结果添加到数据帧中即可进行简单的修改。

#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"))

引用开始和结束的数据

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"))

将它们添加到数据框

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"

结果

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