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 i.e. 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