r - time series padding with NA

2020-02-12 15:14发布

问题:

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

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?

回答1:

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 time-series