R:填写时间序列中缺少的日期?

时间:2022-12-05 23:41:23

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代替具有las遮挡的NA。

I remove the syntetic chron sequence.

我删除了syntetic chron序列。

Can I do same easier? Maybe with some index function related to the frequency?

我可以更容易吗?也许有一些与频率相关的指数函数?

2 个解决方案

#1


7  

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 

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"))

#2


5  

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.

这在动物园常见问题解答http://cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdf的问题13中有所涉及,它使用na.locf的xout =参数来消除合并步骤。由于最近添加了此功能,请确保您使用的是动物园1.6.4或更高版本。

#1


7  

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 

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"))

#2


5  

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.

这在动物园常见问题解答http://cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdf的问题13中有所涉及,它使用na.locf的xout =参数来消除合并步骤。由于最近添加了此功能,请确保您使用的是动物园1.6.4或更高版本。