I have an irregular time series that I’m working with that I’d like to convert to a regular one, but rather than the usual “data missing” behavior answered in other questions, I need to have the observation at each regularly spaced interval be the most recent observation, regardless of how long ago it was. I’ve written a function to do this, but with two loops it’s incredibly slow.
As an example, instead of having
> x <- zoo(c(1, 3, 6), c(1981, 1984, 1985))
> as.ts(x)
Time Series:
Start = 1981
End = 1985
Frequency = 1
[1] 1 NA NA 3 6
I would like a result like this:
> as.ts(x)
Time Series:
Start = 1981
End = 1985
Frequency = 1
[1] 1 1 1 3 6
You can use
na.locffrom the zoo package.