4

我正在尝试将时间序列(动物园对象)提前 7 天(交易周)转移到未来。

library(quantmod)
getSymbols(c("AAPL"), from="2013-01-01", return.class="zoo")
aapl <- Ad(AAPL)
tail(aapl)
2013-05-07 2013-05-08 2013-05-09 2013-05-10 2013-05-13 2013-05-14 
    455.64     460.79     456.77     452.97     454.74     443.86 

I want this:
2013-05-14 2013-05-15 2013-05-16 2013-05-17 2013-05-20 2013-05-21 
    455.64     460.79     456.77     452.97     454.74     443.86 

这有可能吗?我尝试了很多,但都失败了...

4

1 回答 1

2

将其转换为 zooreg 类(以便它知道其规则间隔或几乎如此),然后适当地滞后:

> lag(as.zooreg(tail(aapl)), -7)
2013-05-14 2013-05-15 2013-05-16 2013-05-17 2013-05-20 2013-05-21 
    455.64     460.79     456.77     452.97     454.74     443.86 

有关更多信息,请查看示例部分?zooreg并阅读从其内容开始的评论:

# lag and diff (as diff is defined in terms of lag)
于 2013-05-15T17:13:52.810 回答