title: select column values based on max date and closest date
body: <p>I have a dataset as follows</p>
  
  <pre><code>Id    BMI    Date        Case   CaseDate 
  1331  28.2   2012-05-15  No     NA
  1331  26.4   2011-04-06  No     NA
  1331  29.37  2014-04-01  No     NA
  5074  30.02  2009-10-23  Yes    2014-08-06
  5074  25.12  2011-07-15  Yes    2014-08-06
  </code></pre>
  
  <p>What I am trying to do is as follows</p>
  
  <p>1) For Id where <code>Case=No</code>, Chose BMI that is closest to current date <code>(Sys.Date ())</code> and store this value in a new column (<code>New_BMI</code>)</p>
  
  <p>2) For Id where <code>Case=Yes</code>, Chose BMI that is closest to date in the <code>CaseDate</code> column and store this value in new column (<code>New_BMI</code>)</p>
  
  <p>The final output should look like this below.</p>
  
  <pre><code>Id    BMI    Date        Case   CaseDate     New_BMI
  1331  28.2   2012-05-15  No     NA           29.37
  1331  26.4   2011-04-06  No     NA           29.37
  1331  29.37  2014-04-01  No     NA           29.37
  5074  30.02  2009-10-23  Yes    2014-08-06   25.12
  5074  25.12  2011-07-15  Yes    2014-08-06   25.12
  </code></pre>
  
  <p>The value of <code>New_BMI</code> for Id 1331 is 29.37 because this was the value closest to current date. The values of <code>New_BMI</code> for Id 5074 is 25.12 because this was the value closest to <code>CaseDate</code> (2014-08-06).</p>
  
  <p>Any help on how to compute this <code>New_BMI</code> is much appreciated folks.</p>