If you want to replace NAs with fixed specified values, I think merge
is the way to go. You can make some simplifications though: you don't need the 'zero-column' in z2, and you can fill with zeros in the merge
step:
# input as per question
z1 <- zoo(c(1,2,3,4,5),
as.Date(c('2013-08-09', '2013-08-12', '2013-09-02', '2013-09-09', '2013-09-15')))
start <- as.Date('2013-08-01')
end <- as.Date('2013-09-30')
tt <- seq(start, end, by = "day")
merge(z1, zoo(, tt), fill = 0)
On the other hand, if you want to replace NAs by the last previous non-NA (na.locf
), then the xout
argument may be a way to specify which date range to use for extra- and interpolation, and you don't need merge
. For example:
na.locf(z1, xout = tt)