I am trying to join two dataframes together by their dates. The complicating factor is the two dataframes look slightly different. I'll use the sample data from a previous post:
> eventdates
# A tibble: 2 × 4
  event.no dr.rank   dr.start     dr.end
     <int>   <int>     <date>     <date>
1        1      14 1964-09-30 1964-10-06
2        2      16 1964-11-01 1964-12-24
> ts1964 <- data_frame(DATE = seq(from = as.Date("1964-01-01"), 
+                                 to = as.Date("1964-12-31"), 
+                                 by = "days"),
+                      Q = 1:366)
> 
I was planning to use lapply to create a list that lets me expand out the data from eventdates:
lapply(split(eventdates, seq(nrow(eventdates))), 
       function(x) { 
         filter(ts1964, DATE >= x$dr.start & DATE <= x$dr.end) })
This works in expanding out the dates from eventdates, and getting the column names correct. However, I have realised that this does not retain the event.no grouping variable, nor does it successfully unlist into a dataframe, and melt does not seem to work either.
My question is, how can I join these two dataframes together? Essentially, I require the ts1964 dataframe to have an event.no column (where there is no event, the event.no can be zero or NA etc.)
#
A slice of the expected output should look something like this:
> output <-
+   ts1964 %>%
+   mutate(event.no = 0)
> output$event.no[274:280] <- 1
> output$event.no[306:359] <- 2
> output %>%
+   slice(270:290)
# A tibble: 21 × 3
         DATE     Q event.no
       <date> <int>    <dbl>
1  1964-09-26   270        0
2  1964-09-27   271        0
3  1964-09-28   272        0
4  1964-09-29   273        0
5  1964-09-30   274        1
6  1964-10-01   275        1
7  1964-10-02   276        1
8  1964-10-03   277        1
9  1964-10-04   278        1
10 1964-10-05   279        1
# ... with 11 more rows
> 
 
     
    