【发布时间】:2018-04-23 12:10:03
【问题描述】:
我希望将“最后一次观察结转 (LOCF)”从一个 xts 时间序列转移到另一个包含我想使用的时间戳的 xts 系列。我尝试过使用na.locf,但xout 似乎不起作用,正如我从na.approx 所知道的那样。
任何建议将不胜感激!
这是一个例子:
我有两个 xts 时间序列数据集。一个带有我的数据,另一个带有我想使用的时间戳。
xts_with_data <- as.xts(read.zoo(text='
2016-07-01 00:00:10, 1.0
2016-07-01 00:00:14, 2.0
2016-07-01 00:00:20, 3.0
2016-07-01 00:00:29, 4.0
2016-07-01 00:00:34, 5.0
2016-07-01 00:00:39, 6.0
', sep=',', index=1, tz='', format="%Y-%m-%d %H:%M:%S"))
names(xts_with_data) <- c('x')
xts_with_timestamps <- as.xts(read.zoo(text='
2016-07-01 00:00:15, 0.0
2016-07-01 00:00:20, 0.0
2016-07-01 00:00:30, 0.0
2016-07-01 00:00:35, 0.0
2016-07-01 00:00:38, 0.0
', sep=',', index=1, tz='', format="%Y-%m-%d %H:%M:%S"))
我想要的是这个:
[,1]
2016-07-01 00:00:15 2
2016-07-01 00:00:20 3
2016-07-01 00:00:30 4
2016-07-01 00:00:35 5
2016-07-01 00:00:38 5
(即来自xts_with_timestamps 的时间戳和来自xts_with_data 的相应locf)。
我想我可以像这样使用xout 来做到这一点(这对na.approx 很有效):
na.locf(xts_with_data, xout = index(xts_with_timestamps))
但这只是返回我原来的xts_with_data。
有什么建议吗?
提前致谢。
【问题讨论】:
-
看来您的问题根源在于
xts:::na.locf.xts中缺少的功能。如果Joshua(开发人员)还不知道的话,可能值得让他知道。