title: R - Number of days since last occurrence
body: <p>I have a data frame that looks like this:</p>
  
  <pre><code> id     date
   1001   2012-10-11
   1005   2013-02-20
   1005   2012-11-21
   1005   2014-03-14
   1003   2013-10-25
   1003   2013-11-30
  </code></pre>
  
  <p>I need to find, for each row, the number of days that have passed since the last occurrence of that id. For the above example, the answer would look like this:</p>
  
  <pre><code> id     date        no_of_days
   1001   2012-10-11  NA
   1005   2013-02-20  91
   1005   2012-11-21  NA
   1005   2014-03-14  387
   1003   2013-10-25  NA
   1003   2013-11-30  36
  </code></pre>
  
  <p>A bit of searching got me to the point where I can add a new column with values that were generated by applying a function on subgroups (the R equivalent of STATA's "bysort"):</p>
  
  <pre><code>df$no_of_days&lt;-with(df,ave(id,id,FUN=days_passed,na.rm=TRUE))
  </code></pre>
  
  <p>However, defining the new function days_passed is proving to be tricky as I have to find the last occurrence of that uniqid, and then formulate the function accordingly.</p>
  
  <p>I'm new to R, so any help on this would be greatly appreciated. Thanks!</p>