简体   繁体   中英

Fill gaps between dates in xts

So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:

i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
       seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

ts <- xts(rep(0,length(i)), order.by = i)

            [,1]
2015-01-01    0
2015-02-01    0
2015-03-01    0
2015-04-01    0
2015-05-01    0
2015-06-01    0
2015-07-01    0
2015-08-01    0
2015-09-01    0
2015-10-01    0
2015-11-01    0
2015-12-01    0
2016-01-01    0
2017-01-01    0
2017-02-01    0
2017-03-01    0
2017-04-01    0
2017-05-01    0
2017-06-01    0
2017-07-01    0
2017-08-01    0
2017-09-01    0
2017-10-01    0
2017-11-01    0
2017-12-01    0
2018-01-01    0

What I wish to achieve is "fill" the time series ts for all months between two arbitrary dates ie start.date and end.date with a 1 . Any suggestions?

My attempt:

  if(index(ts)[1] > start.date){
    len.aux <- length(seq(from = start.date, to = index(ts)[1] %m-% months(1), by = "month"))
    ts <- c(xts(rep(1, len.aux), order.by = seq.Date(from = start.date, to = index(ts)[1] %m-% months(1), by = "month")), ts)
  } 
  if(index(ts)[length(ts)] < end.date){
    len.aux <- length(seq(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month"))
    ts <- c(ts, xts(rep(1, len.aux), order.by = seq.Date(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month")))
  } 

However, this only fills the gaps for the "tails" of the series and doesn't fill gaps in between.

Thanks for the help!

Please note that this is only a minimal working example of my issue

You may use the tsibble package:

library(tsibble)
i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
       seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

tsibble(datetime = yearmonth(i), 
        value    = 0, index = datetime) %>% 
  fill_gaps(value = 1) %>% 
  View()

The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM