I have two datasets that I would like to merge using inner_join in R. The problem is that the second dataset contains dates ranges, and I would like to keep this information. How can I match the date of the 1st dataset with the date-range of the 2nd dataset? Below a working example.
Thank you very much.
library(data.table)
library(dplyr)
# First Dataset
dt_1 <- data.table()
dt_1$city <- c("madrid","milan","milan","paris", "Rome")
dt_1$address <- c("a","a","b","c","d")
dt_1$date_1 <- c( "2017", "2013", "2008", "1901","2009")
dt_1
# Second dataset
dt_2 <- data.table()
dt_2$city <- c("milan","madrid","Porto","Barcelona", "Rome")
dt_2$address <- c("a","a","b","c","d")
dt_2$date_1 <- c( "2012", "2016", "2006", "1900","2009")
dt_2$date_2 <- c( "2015", "NA", "2022", "1930","NA")
dt_2
## How to match the corresponding exact dates of the two datasets BUT ALSO the dates falling -
## in the ranges
# This keeps only if the first date is the same
dt_match <- inner_join(dt_1, dt_2, by = c("city","address","date_1"), keep = TRUE)
# How to achieve this ?
dt_match <- data.table()
dt_match$city <- c("milan","Rome")
dt_match$address <- c("a","c")
dt_match$date <- c( "2013","2009")
dt_match