I have two data frames mondays & tdates as follows :
T Dates
User.ID tdate
1 11-02-2013
1 04-03-2013
1 16-04-2015
1 03-05-2015
1 05-05-2015
1 11-05-2015
1 29-09-2015
1 26-11-2013
1 28-11-2013
3 01-02-2016
4 22-11-2012
4 25-04-2013
4 29-05-2013
Mondays
ID Monday Closest Date
1 05-09-2016
1 20-04-2015
1 27-07-2015
1 08-06-2015
1 13-10-2014
3 16-09-2013
3 16-02-2015
3 29-08-2016
3 26-05-2014
3 29-02-2016
3 18-07-2016
3 22-02-2016
4 16-11-2015
Now i want to return the past closest or equivalent date in 3rd column from tdates for each of the User.ID in mondays.
For e.g
the expected output is
Mondays
ID Monday Closest Date
1 05-09-2016 29-09-2015
1 20-04-2015 16-04-2015
1 27-07-2015 11-05-2015
1 08-06-2015 11-05-2015
1 13-10-2014 28-11-2013
3 16-09-2013 NA
3 16-02-2015 NA
3 29-08-2016 01-02-2016
3 26-05-2014 NA
3 29-02-2016 01-02-2016
3 18-07-2016 01-02-2016
3 22-02-2016 01-02-2016
4 16-11-2015 29-05-2013
For ID = 1 & Monday = 05-09-2016
the past closest tdate is 29-09-2015 thus it'll get this date in Closest Date column
Note : If no transaction date is found to past or equivalent to monday's date fill NAs
This has to be done for a very large data set , any ideas how this can be done . I have tried this using a customized function as follows :
lasttxndate <- function(userid, mydate){
+ return(max(subset(tdates$Date.Asked, tdates$User.ID == userid & tdates$Date.Asked <= as.Date(mydate))))
+ }
But this isn't working out when using this with lapply' orsapply`.