title: Filter eventlogs that are within a time-interval in R using dplyr
body: <p>I have an event log in the format below. </p>
  
  <p><a href="https://i.stack.imgur.com/4mRYz.png" rel="nofollow noreferrer">Original format</a><br>
  I have created groups by DATE and ID using dplyr,hence a change in either date or ID will be taken as a different group.</p>
  
  <p>I want to have only events that are >= 5secs time interval and remove the rest.
  <a href="https://i.stack.imgur.com/zuJE8.png" rel="nofollow noreferrer">Desired output</a></p>
  
  <p>I have used dplyr and time lag to acheive this,since I am not able to dynamically assign a lag interval for this. But my current code checks for one lag interval and I end up deleting more rows than desired.<a href="https://i.stack.imgur.com/7wtMK.png" rel="nofollow noreferrer">Current output - all rows in yellow are removed</a>. Ideally I would want "13:10:22",
  "13:10:24" in group 2 to be retained since the time lag from "13:10:17" to these times is 5 secs and more.</p>
  
  <p>I am using "chron" to handle the times.
  I understand the time lag logic will not work in mycase. Could there be a better alternative apart from using an expensive for/if loop.</p>
  
  <p>Code I have used  </p>
  
  <pre><code>data$Date &lt;- as.Date(data$Date,format = "%m/%d/%Y")  
  data$Time &lt;- chron(times = data$Time)  
  
  data &lt;- data  %&gt;% arrange(Date,Time,ID)    
  data$Group &lt;- data %&gt;%  group_by(Date,ID) %&gt;% group_indices    
  data &lt;- data %&gt;%     
          group_by(Group)  %&gt;%       
          mutate(time.difference = Time - lag(Time)) %&gt;%    
          filter(time.difference &gt;= 0.00005787 | is.na(time.difference))  
  </code></pre>
  
  <p><strong>Dput of the data</strong>   </p>
  
  <p>structure(list(Date = structure(c(17469, 17469, 17469, 17469, 
  17469, 17469, 17469, 17469, 17469, 17469, 17469, 17469, 17469, 
  17469, 17469, 17470, 17470, 17470, 17470), class = "Date"), Time = structure(c(0.936400462962963, 
  0.9425, 0.9425, 0.942511574074074, 0.942523148148148, 0.9703125, 
  0.548518518518519, 0.548530092592593, 0.54880787037037, 0.54880787037037, 
  0.548819444444444, 0.548842592592593, 0.548865740740741, 0.548888888888889, 
  0.557337962962963, 0.6140625, 0.618761574074074, 0.618958333333333, 
  0.622303240740741), format = "h:m:s", class = "times"), ID = c("P1", 
  "P1", "P1", "P1", "P1", "P1", "P5", "P5", "P5", "P5", "P5", "P5", 
  "P5", "P5", "P5", "P9", "P9", "P9", "P9")), .Names = c("Date", 
  "Time", "ID"), row.names = c(NA, -19L), class = "data.frame")</p>