问题
Hi I have a time series dataset contains a few data points in Aug and Sep.
How can I fill in the missing days with a default value easily, say 0 in this case:
What I am thinking right now to merge
the dataset with a sequential time series for the timeline I like, then do na.fill
to replace NAs with the default value I want.
This is what I have done:
# This is my data z1
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')))
# This is the timeline I want
z2 <- zoo(0, seq(from=as.Date('2013-08-01'), to=as.Date('2013-09-30'), by="day"))
# This is my result
na.fill(merge(z1, z2)[,1], 0)
But I am wondering is there a function already existing to do what I want. Something like:
result <- foo_fill(z1, 0, start, end)
回答1:
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)
来源:https://stackoverflow.com/questions/19575315/expand-zoo-timeline-in-r