I solved it with a little gambit. I imagine there must be some simpler way, but this below is working as it should for at least this example.
First, I created the data frames a
and b
, as in the question example:
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 data frame, called c
. I called these dates data_final
. I created a daily sequence, starting at least from 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 the dates stored within data_final
and only NA
in the value column:
c <- data.frame(data=data_final, valor=NA)
This ugly, just refreshed the column positions 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
Just to clarify: the column
valor
of data framec
has these values of the issue(2, 2, 2, NA, 3, 6, 2)
or should be the original values ofa
andb
, interspersed by aNA
?– Marcus Nunes
exactly fixed the question
– Artur_Indio
A
dput()
would help– Tomás Barcellos