1

I have a vector of numbers and a corresponding vector of dates (monthly). Some months are missing so I would like to create a time series object that includes NA for the missing dates.

x = c(1,2,3,4)
dates = c('2000-01-01','2000-02-01','2000-04-01','2000-07-01')

Is there an easy way to get a time series object that goes from '2000-01-01' to '2000-07-01' that includes NAs for the missind dates?

Christine Braun
  • 157
  • 1
  • 1
  • 8

1 Answers1

5

You can use padr package to do that

df <- data.frame(x = c(1,2,3,4),
                dates = c('2000-01-01','2000-02-01','2000-04-01','2000-07-01'))

library(padr)

df %>% 
  mutate(dates = as.Date(dates)) %>%
  pad()

pad applied on the interval: month
   x      dates
1  1 2000-01-01
2  2 2000-02-01
3 NA 2000-03-01
4  3 2000-04-01
5 NA 2000-05-01
6 NA 2000-06-01
7  4 2000-07-01
Tung
  • 26,371
  • 7
  • 91
  • 115