title: Calculate elapsed time since last event
body: <p>I have a data frame that contains multiple subjects (<code>id</code>), with repeated observations (recorded at times <code>time</code>). Each of the times may or may not be associated with an event (<code>event</code>).  An example data frame can be generated with:</p>
  
  <pre><code>set.seed(12345)
  id &lt;- c(rep(1, 9), rep(2, 9), rep(3, 9))
  time &lt;- c(seq(from = 0, to = 96, by = 12),
        seq(from = 0, to = 80, by = 10),
        seq(from = 0, to = 112, by = 14))
  random &lt;- runif(n = 27)
  event &lt;- rep(100, 27)
  
  df &lt;- data.frame(cbind(id, time, event, random))
  df$event &lt;- ifelse(df$random &lt; 0.55, 0, df$event)
  df &lt;- subset(df, select = -c(random))
  df$event &lt;- ifelse(df$time == 0, 100, df$event)
  </code></pre>
  
  <p>I would like to calculate the time between events (<code>tae</code> [time after the last event]), such that the ideal output would look like:</p>
  
  <pre><code>head(ideal_df)
    id time event tae
  1  1    0   100   0
  2  1   12   100   0
  3  1   24   100   0
  4  1   36   100   0
  5  1   48     0  12
  6  1   60     0  24
  </code></pre>
  
  <p>In fortran, I use the following code to create the <code>tae</code> variable:</p>
  
  <pre><code>IF(EVENT.GT.0) THEN
    TEVENT = TIME
    TAE = 0
  ENDIF
  
  IF(EVENT.EQ.0) THEN
    TAE = TIME - TEVENT
  ENDIF
  </code></pre>
  
  <p>In R, I have attempted both an <code>ifelse</code> and <code>dplyr</code> solution. However, neither produce my desired output.</p>
  
  <pre><code># Calculate the time since last event (using ifelse)
  df$tae &lt;- ifelse(df$event &gt;= 0, df$tevent = df$time &amp; df$tae = 0, df$tae = df$time - df$tevent)
  
  Error: unexpected '=' in "df$tae &lt;- ifelse(df$event &gt;= 0, df$tevent ="
  
  # Calculate the time since last event (using dplyr)
  res &lt;- df %&gt;%
    arrange(id, time) %&gt;%
    group_by(id) %&gt;%
    mutate(tae = time - lag(time))
  res 
  
     id time event tae
  1   1    0   100  NA
  2   1   12   100  12
  3   1   24   100  12
  4   1   36   100  12
  5   1   48     0  12
  6   1   60     0  12
  </code></pre>
  
  <p>Clearly, neither of these yield my desired output.  It appears as though assigning variables within the <code>ifelse</code> function is not well tolerated by R.  My attempt at a <code>dplyr</code> solution also fails to account for the <code>event</code> variable...</p>
  
  <p>Lastly, another variable that recorded the time until the next event <code>tue</code> will be needed.  If anyone happens to have a thought regarding how best to go about this (perhaps more tricky) calculation, please feel free to share.</p>
  
  <p>Any thoughts regarding how to get one of these working (or an alternative solution) would be greatly appreciated.  Thanks!</p>
  
  <p>P.S. -- A reproducible example when the interval between events changes within an <code>ID</code> is presented below:  </p>
  
  <pre><code>id &lt;- rep(1, 9)
  time &lt;- c(0, 10, 22, 33, 45, 57, 66, 79, 92)
  event &lt;- c(100, 0, 0, 100, 0, 100, 0, 0, 100)
  df &lt;- data.frame(cbind(id, time, event))
  
  head(df)
    id time event
  1  1    0   100
  2  1   10     0
  3  1   22     0
  4  1   33   100
  5  1   45     0
  6  1   57   100
  </code></pre>