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

时间:2015-02-11 13:09:04

标签: r time-series xts zoo rollapply

虚拟动物园对象创建为

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列/结束窗口),如下所示[使用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

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

2 个答案:

答案 0 :(得分:3)

1) zoofortify.zoo方法生成一个Index列的数据框,因此假设rrollapply的输出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 在问题中给出。然后,对于宽度为3,结束日期是相应开始日期之后的2天,所以:

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

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

r

2)假设上面有data.frame(start_date = time(r), end_date = time(r) + 2, mean = coredata(r)) ,这也可行:

{{1}}

答案 1 :(得分:2)

只需将两个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