ska*_*kan 17 r time-series fill zoo
我有一个动物园时间序列,错过了几天.为了填补它并有一个连续的系列我做...
我从头到尾生成一个chron日期时间序列.
我把我的系列与这个合并.
我使用na.locf代替具有las遮挡的NAs.
我删除了syntetic chron序列.
我可以更容易吗?也许有一些与频率相关的指数函数?
如果使用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
Run Code Online (Sandbox Code Playgroud)
编辑:对于日内数据(使用Gabor的优秀xout=建议):
> index(x) <- as.POSIXct(index(x))
> na.locf(x, xout=seq(head(index(x),1),tail(index(x),1),by="15 min"))
Run Code Online (Sandbox Code Playgroud)
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. Be sure you are using zoo 1.6.4 or later since this feature was added recently.