my problem is simple. I have 2 data frame, each of them has a a column of dates (%Y-%m-%d), and a column for the ids. One has just one id per row, the other one has multiple rows for the same id. I want to take the value so that it presents the minumum difference for the dates. Now I explain better with an example:
df1 (single values for colA):
+-------+------------+------+------+-------+-------+
| colA | colB | colC | colD | colE | colF |
+-------+------------+------+------+-------+-------+
| 3000 | 2011-01-20 | 2 | 3.43 | 2.01 | 1.63 |
| 3001 | 2012-04-06 | 1 | 1.12 | -0.63 | -1.16 |
| 3002 | 2012-04-24 | 2 | 2.28 | -0.18 | -0.12 |
| 3003 | 2012-04-13 | 2 | 1.27 | -0.51 | -0.82 |
| 3004 | 2011-08-24 | 5 | 5.30 | 2.68 | 2.10 |
| 3006 | 2011-08-02 | 2 | 2.12 | -0.27 | -2.60 |
+-------+------------+------+------+-------+-------+
df2 (multiple values for first column (X)):
+------+---------------+----------+
| colX | colY | colZ |
+------+---------------+----------+
| 3000 | 2011-02-01 | 0 |
| 3000 | 2012-03-01 | 0 |
| 3000 | 2013-02-01 | 0 |
| 3000 | 2014-03-01 | 1 |
| 3000 | 2015-03-01 | 0 |
| 3000 | 2016-04-01 | 0 |
| 3002 | 2011-03-01 | 1 |
| 3002 | 2011-08-01 | 1 |
| 3002 | 2012-04-01 | 0 |
+------+---------------+----------+
In this case I see the first value in colA (df1) and compute all the differences in months between 2011-01-20 with all of the dates for 3000 in df2 (2011-02-01, 2012-03-01,ecc), so the first 6 rows. I take only the minimum difference, so in this case is the first one (2011-02-01) that is almost one month. So at the end I should have df1 with 3 new columns (Y and Z and diff) so the minimum date on df2, 0/1 of Z and the difference of the 2 date in days.
e.g. for 3000 (for the difference I take the abs):
3000 2011-01-20 2 3.43 2.01 1.63 2011-02-01 0 12
What function should I use? apply? ddply?
Thanks in advance