简体   繁体   English

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... 为了填补它并有一个连续的系列我做...

I generate a chron date-time sequence from start to end. 我从头到尾生成一个chron日期时间序列。

I merge my series with this one. 我把我的系列与这个合并。

I use na.locf to substitute NAs with las obsservation. 我使用na.locf来替代具有拉斯维加斯的NA。

I remove the syntetic chron sequence. 我删除了syntetic chron序列。

Can I do same easier? 我可以更容易吗? Maybe with some index function related to the frequency? 也许有一些与频率相关的指数函数?

It's slightly easier if you use a "empty" zoo object with an index. 如果使用带有索引的“空” zoo对象,则会稍微容易一些。

> 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 

EDIT: For intra-day data (using Gabor's excellent xout= suggestion): 编辑:对于日内数据(使用Gabor的优秀xout=建议):

> index(x) <- as.POSIXct(index(x))
> na.locf(x, xout=seq(head(index(x),1),tail(index(x),1),by="15 min"))

This is covered in question 13 of the zoo FAQ http://cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdf which uses the xout= argument of na.locf to eliminate the merge step. 这在动物园常见问题解答http://cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdf的问题13中有所涉及,它使用na.locf的xout =参数来消除合并步骤。 Be sure you are using zoo 1.6.4 or later since this feature was added recently. 由于最近添加了此功能,请确保您使用的是动物园1.6.4或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM