R:在时间序列中填充缺失的日期? [英] R: Filling missing dates in a time series?
问题描述
我有一个缺少天数的动物园时间序列.为了填补它并有一个连续的系列,我这样做......
I have a zoo time series with missing days. In order to fill it and have a continuous series I do...
我从开始到结束生成一个 chron 日期时间序列.
I generate a chron date-time sequence from start to end.
我将我的系列与这个合并.
I merge my series with this one.
我使用 na.locf 将 NA 替换为 las 观察.
I use na.locf to substitute NAs with las obsservation.
我删除了 syntetic chron 序列.
I remove the syntetic chron sequence.
我可以更轻松地做同样的事情吗?也许有一些与频率相关的索引函数?
Can I do same easier? Maybe with some index function related to the frequency?
推荐答案
如果使用带有索引的空"zoo
对象会稍微容易一些.
It's slightly easier if you use a "empty" zoo
object with an index.
> x <- zoo(1:10,Sys.Date()-10:1)[c(1,3,5,7,10)]
> empty <- zoo(order.by=seq.Date(head(index(x),1),tail(index(x),1),by="days"))
> na.locf(merge(x,empty))
2010-08-14 2010-08-15 2010-08-16 2010-08-17 2010-08-18
1 1 3 3 5
2010-08-19 2010-08-20 2010-08-21 2010-08-22 2010-08-23
5 7 7 7 10
对于日内数据(使用 Gabor 出色的 xout=
建议):
For intra-day data (using Gabor's excellent xout=
suggestion):
> index(x) <- as.POSIXct(index(x))
> na.locf(x, xout=seq(head(index(x),1),tail(index(x),1),by="15 min"))
这篇关于R:在时间序列中填充缺失的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!