I'd like to join two tables together where the join is based on a date in a date field falling in a particular (start_date, end_date) range. Is there a nice elegant way to do this in R (via dplyr or otherwise).
So that if for example I have:
x <- data.frame(
Ref = c(1:5),
Event_Date = as.Date(c("2014-03-31", "2013-12-01", "2016-07-13", "2017-04-13", "2015-02-01"))
)
y <- data.frame(
Period_ID = c(2013:2016),
Period_Start = as.Date(c("2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01")),
Period_End = as.Date(c("2013-12-31", "2014-12-31", "2015-12-31", "2016-12-31"))
)
Then I'd like to run a left join such that:
z = left_join(x, y, by = ???)
but where the match is not "Date = Lookup Date" but "Date falls in the range (Start_Date, End_Date)
In other words. what I'd like to see is:
Thank you.