title: Calculate time difference between two events while disregarding unmatched events
body: <p>I have a data set with a structure such as this:</p>
  
  <pre><code>structure(list(id = c(43956L, 46640L, 71548L, 71548L, 71548L, 
  72029L, 72029L, 74558L, 74558L, 100596L, 100596L, 100596L, 104630L, 
  104630L, 104630L, 104630L, 104630L, 104630L, 104630L, 104630L
  ), event = c("LOGIN", "LOGIN", "LOGIN", "LOGIN", "LOGOUT", "LOGIN", 
  "LOGOUT", "LOGIN", "LOGOUT", "LOGIN", "LOGOUT", "LOGIN", "LOGIN", 
  "LOGIN", "LOGIN", "LOGIN", "LOGIN", "LOGOUT", "LOGIN", "LOGOUT"
  ), timestamp = c("2017-03-27 09:19:29", "2016-06-10 00:09:08", 
  "2016-01-27 12:00:25", "2016-06-20 11:34:29", "2016-06-20 11:35:44", 
  "2016-12-28 10:43:25", "2016-12-28 10:56:30", "2016-10-15 15:08:39", 
  "2016-10-15 15:10:06", "2016-03-09 14:30:48", "2016-03-09 14:31:10", 
  "2017-04-03 10:36:54", "2016-01-11 16:52:08", "2016-02-03 14:40:32", 
  "2016-03-30 12:34:56", "2016-05-26 13:14:25", "2016-08-22 15:20:02", 
  "2016-08-22 15:21:53", "2016-08-22 15:22:23", "2016-08-22 15:23:08"
  )), .Names = c("id", "event", "timestamp"), row.names = c(5447L, 
  5446L, 5443L, 5444L, 5445L, 5441L, 5442L, 5439L, 5440L, 5436L, 
  5437L, 5438L, 5425L, 5426L, 5427L, 5428L, 5429L, 5430L, 5431L, 
  5432L), class = "data.frame")
  
           id  event           timestamp
  5447  43956  LOGIN 2017-03-27 09:19:29
  5446  46640  LOGIN 2016-06-10 00:09:08
  5443  71548  LOGIN 2016-01-27 12:00:25
  5444  71548  LOGIN 2016-06-20 11:34:29
  5445  71548 LOGOUT 2016-06-20 11:35:44
  5441  72029  LOGIN 2016-12-28 10:43:25
  5442  72029 LOGOUT 2016-12-28 10:56:30
  5439  74558  LOGIN 2016-10-15 15:08:39
  5440  74558 LOGOUT 2016-10-15 15:10:06
  5436 100596  LOGIN 2016-03-09 14:30:48
  5437 100596 LOGOUT 2016-03-09 14:31:10
  5438 100596  LOGIN 2017-04-03 10:36:54
  5425 104630  LOGIN 2016-01-11 16:52:08
  5426 104630  LOGIN 2016-02-03 14:40:32
  5427 104630  LOGIN 2016-03-30 12:34:56
  5428 104630  LOGIN 2016-05-26 13:14:25
  5429 104630  LOGIN 2016-08-22 15:20:02
  5430 104630 LOGOUT 2016-08-22 15:21:53
  5431 104630  LOGIN 2016-08-22 15:22:23
  5432 104630 LOGOUT 2016-08-22 15:23:08
  </code></pre>
  
  <p>I wish to calculate the time difference between <code>LOGIN</code> and <code>LOGOUT</code> (session duration) as well as between <code>LOGOUT</code> and <code>LOGIN</code> (session interval). Unfortunately, I have <code>LOGIN</code> events that do not have a matching <code>LOGOUT</code> event.</p>
  
  <p>The correct <code>LOGOUT</code> event always follows its' corresponding <code>LOGIN</code> event (as I ordered the data frame based on <code>id</code> and <code>timestamp</code>. I tried adapting <a href="https://stackoverflow.com/a/40397476/3578190">this answer</a>, but have had no luck. I also tried creating an event identifier, but since I can't find a way to get the numbering for the <code>LOGOUT</code> event to match the numbering for the <code>LOGIN</code> event, I am unsure as to how useful such an identifier will be:</p>
  
  <pre><code>df$eventNum &lt;- as.numeric(ave(as.character(df$id), df$id, as.character(df$event), FUN = seq_along))
  </code></pre>