I would like to have "Last Observation Carried Forward (LOCF)" from one xts time series to another xts series containing the time stamps that I would like to use. I have tried with na.locf
but xout
does not seem to work as I know from na.approx
.
Any suggestions would be appreciated!
Here is an example:
I have two xts time series data sets. One with my data and another with time stamps that I would like to use.
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"))
What I would like, is this:
[,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
(i.e. the time stamp from xts_with_timestamps
and corresponding locf from xts_with_data
).
I thought I could do it with xout
like this (which works fine for na.approx
):
na.locf(xts_with_data, xout = index(xts_with_timestamps))
but that just returns my original xts_with_data
.
Any suggestions?
Thanks in advance.
xts:::na.locf.xts
. It might be worth letting Joshua (the developer) know, if he doesn't already. – AkselA