r - 用 NA 填充的时间序列 [英] r - time series padding with NA

查看:36
本文介绍了r - 用 NA 填充的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如说,如果我有一个如下的数据框:

Say, if I have a data frame as follows:

Date1 <- seq(from = as.POSIXct("2010-05-01 02:00"), 
             to = as.POSIXct("2010-10-10 22:00"), by = 3600)
Dat <- data.frame(DateTime = Date1,
                  x1 = rnorm(length(Date1)))

其中每次测量之间的间隔为 1 小时.怎么可能在一年的剩余时间里用 NA 填充这个数据框,最终解决方案的长度应该是 8760,即整年的每小时测量值.例如,我希望 DateTime 列的范围从 2010-01-01 00:00 到 2010-12-31 23:00,但将 x1 列设为 NA 以表示已添加到原始日期数据框(如果有意义的话).我想提出一个可以有任意年数的解决方案,即如果数据从 2009 年 5 月到 2012 年 9 月,那么最终的解决方案应该有这个数据集,但缺少时间,即从 2009 年 1 月到 2012 年 12 月到用 NA 填充.我该如何解决这个问题?

where the spacing between each measurement is 1 hour. How would it be possible to pad this data frame with NAs for the rest of the year, where the final solution should have a length of 8760 i.e. hourly measurements for the entire year. I would like to have the DateTime column to extent from 2010-01-01 00:00 to 2010-12-31 23:00, for example, but have the x1 column to be NA for the days that have been added to the original data frame (if that makes sense). I would like to come up with a solution where there can be any number of years i.e. if the data extends from May 2009 to September 2012 then the final solution should have this data set but with the missing times i.e. from January 2009 to December 2012 to be padded with NA's. How can I go about solving this issue?

推荐答案

创建包含所有小时的新数据框,然后合并两个数据框.

Create new data frame that contains all hours and then merge both data frames.

df2<-data.frame(DateTime=seq(from = as.POSIXct("2010-01-01 00:00"), 
                             to = as.POSIXct("2010-12-31 23:00"), by = "hour"))
merge(df2,Dat,all=TRUE)

这篇关于r - 用 NA 填充的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆