I resolved with a little gambiarra. I imagine there should be some simpler way, but this one below is working as it should for at least this example.
First, I created the data frames a
and b
, as in the example of the question:
a <- data.frame(data=seq(from=as.Date("01-02-2010", format="%m-%d-%Y"),
to=as.Date("01-04-2010", format="%m-%d-%Y"), "days"), valor=c(2, 0, 9))
b <- data.frame(data=seq(from=as.Date("01-06-2010", format="%m-%d-%Y"),
to=as.Date("01-08-2010", format="%m-%d-%Y"), "days"), valor=c(3, 6, 2))
Next, I created all the dates that should appear in the final frame data, called c
. I called these dates data_final
. I created a daily sequence, starting at the very least of the dates and ending at most of them:
data_final <- seq(from=min(a$data, b$data), to=max(a$data, b$data), "days")
Then just create the data frame c
. The first version of it has dates stored inside data_final
and only NA
in the value column:
c <- data.frame(data=data_final, valor=NA)
This ugly, only updating the positions of column valor
which have equivalent dates in a
and b
:
c$valor[data_final %in% a$data] <- a$valor[a$data %in% data_final]
c$valor[data_final %in% b$data] <- b$valor[b$data %in% data_final]
c
data valor
1 2010-01-02 2
2 2010-01-03 0
3 2010-01-04 9
4 2010-01-05 NA
5 2010-01-06 3
6 2010-01-07 6
7 2010-01-08 2