title: R - Calculate Time Elapsed Since Last Events with Multiple Event Types and IDs
body: <p>Similar questions have been asked before where the question is how to calculate the number of observations since an event. I have a further request. How to calculate the number of days since the same type of observation but also to find the number of days since any other type of event. I also have ids.  </p>
  
  <p>To illustrate please see below. I am trying to do this in R using Datatables but to little result. </p>
  
  <p>What I have:</p>
  
  <pre><code>  date     event  id
  2000-07-06     2  1
  2000-07-07     1  1
  2000-07-09     0  1
  2000-07-10     0  1
  2000-07-15     2  1
  2000-07-16     1  1
  2000-07-20     0  1
  2000-07-21     1  1
  2000-07-06     1  2
  2000-07-07     2  2
  2000-07-15     0  2
  2000-07-16     0  2
  2000-07-17     2  2
  2000-07-18     1  2
  </code></pre>
  
  <p>and what I would like to have is as follows: </p>
  
  <pre><code>  date     event  id days_since_event_1 days_since_event_2
  2000-07-06     2  1        NA                 NA
  2000-07-07     1  1        NA                 1
  2000-07-09     0  1        2                  3
  2000-07-10     0  1        3                  4
  2000-07-15     2  1        8                  9
  2000-07-16     1  1        9                  1
  2000-07-20     0  1        4                  5
  2000-07-21     1  1        5                  6
  2000-07-06     1  2        NA                 NA
  2000-07-07     2  2        1                  NA
  2000-07-15     0  2        9                  8
  2000-07-16     0  2        10                 9
  2000-07-17     2  2        11                 10
  2000-07-18     1  2        12                 1
  </code></pre>
  
  <p>The two events are mutually exclusive, that is, they cannot take place on the same day. 
  Hope to hear some good advice. All the best. </p>