title: Eliminate duplicates based on 2 dates in a dataframe
body: <p>I have this sample dataframe:</p>
  <pre><code>df &lt;- data.frame(ID = c(&quot;5&quot;,&quot;5&quot;,&quot;5&quot;,&quot;5&quot;,&quot;5&quot;,&quot;5&quot; ,&quot;5&quot;    ,&quot;5&quot;,&quot;5&quot;,&quot;5&quot;,&quot;5&quot;,&quot;14&quot;,&quot;14&quot;,&quot;14&quot;,&quot;14&quot; ,&quot;14&quot;,&quot;14&quot;),
                   Date1= c(&quot;22/07/2014&quot;,&quot;22/07/2014&quot;,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                             ,&quot;22/07/2014&quot;
                            ,&quot;22/07/2014&quot;
                            ,&quot;08/11/2016&quot; 
                           , &quot;08/11/2016&quot;
                           , &quot;08/11/2016&quot;
                           , &quot;08/11/2016&quot;
                           , &quot;08/11/2016&quot;
                           , &quot;08/11/2016&quot;),
                   Date2= c(&quot;01/01/2011&quot;
                            ,&quot;01/08/2011&quot;
                            ,&quot;01/12/2010&quot;
                            ,&quot;10/11/2015&quot;
                            ,&quot;22/07/2014&quot;
                            ,&quot;01/01/2013&quot;
                            ,&quot;23/04/2014&quot;
                            ,&quot;01/01/2006&quot;
                            ,&quot;01/01/2013&quot;
                            ,&quot;01/10/2012&quot;
                            ,&quot;01/08/2012&quot;
                            ,&quot;14/04/2015&quot;
                            ,&quot;01/10/2008&quot;
                            ,&quot;01/10/2008&quot;
                            ,&quot;14/05/2015&quot;
                            ,&quot;11/04/2015&quot;
                            ,&quot;05/10/2008&quot;),
  stringsAsFactors = F)
  </code></pre>
  <p>where I have each ID repeated several times. I need to get a dataframe with only 1 line per ID. As you can see, each patient has only one date in column df$date1, so the condition to select 1 row per patient would be: to pick the <em><strong>closest</strong></em> date between date 1 and date 2.</p>
  <p>How could I do that?</p>
  <p>Thansk</p>