I have two data sets:
table1 <- data.frame(id=c(1000,1001,1002,1003),
date=as.POSIXct(c("2012-05-13","2012-09-23","2011-04-09","2014-11-08")))
table2 <- data.frame(id2=c(1000,1000,1001,1002,1003,1003),
date2=as.POSIXct(c("2012-05-13","2012-05-16","2012-09-24","2011-04-15","2014-11-09", "2014-11-10")))
I want to do a left join on table1 based on matching ID and Date, however not all dates have an exact match so I was wondering how could I join the dates based on the closest day? For example for id 1001, "2012-09-23" would match "2012-09-24" for id2 1001 since it is the only date for id2 and for 1003 the "2014-11-08" would match "2014-11-09" of 1003 for id2 since it is the closest day.
Desired result:
id date date2
1 1000 2012-05-13 2012-05-13
2 1001 2012-09-23 2012-09-24
3 1002 2011-04-09 2011-04-15
4 1003 2014-11-08 2014-11-09