I am looking for a simple method for joining two tables by date range. 1 contains the exact date, another table contains two variables that determine the beginning and end of the time period. I need to join tables if the date in the first table is in the range from the second table.
data1 <- data.table(date = c('2010-01-21', '2010-01-25', '2010-02-02', '2010-02-09'),
name = c('id1','id2','id3','id4'))
data2 <- data.table(beginning=c('2010-01-15', '2010-01-23', '2010-01-30', '2010-02-05'),
ending = c('2010-01-22','2010-01-29','2010-02-04','2010-02-13'),
class = c(1,2,3,4))
result <- data.table(date = c('2010-01-21', '2010-01-25', '2010-02-02', '2010-02-09'),
beginning=c('2010-01-15', '2010-01-23', '2010-01-30', '2010-02-05'),
ending = c('2010-01-22','2010-01-29','2010-02-04','2010-02-13'),
name = c('id1','id2','id3','id4'),
class = c(1,2,3,4))
Any help please? I found some difficult examples, but they do not even work with my data due to the formats. I need something like:
select * from data1
left join
select * from data2
where data2.beginning <= data1.date <= data2.ending
thank