title: Group, take duration and set condition within R (dplyr, r)
body: <p>I have a dataset, df:   (the dataset contains over 4000 rows)</p>
  
  <pre><code>  DATEB
  
    9/9/2019 7:51:58 PM
    9/9/2019 7:51:59 PM
    9/9/2019 7:51:59 PM
    9/9/2019 7:52:00 PM
    9/9/2019 7:52:01 PM
    9/9/2019 7:52:01 PM
    9/9/2019 7:52:02 PM
    9/9/2019 7:52:03 PM
    9/9/2019 7:54:00 PM
    9/9/2019 7:54:02 PM
    9/10/2019 8:00:00PM
  </code></pre>
  
  <p>I am wanting to place these in separate groups, and take the duration, if the time between date-time exceeds 120 seconds.</p>
  
  <p>Desired output:</p>
  
  <pre><code>Group   Duration
  
   a       5 sec
   b       2 sec
   c       0 sec
  
  
  
  
   dput:
  
  
    structure(list(DATEB = structure(c(2L, 3L, 3L, 4L, 5L, 5L, 6L, 
    7L, 8L, 9L, 1L), .Label = c("      9/10/2019 8:00:00 PM", "      9/9/2019 7:51:58 PM", 
    "      9/9/2019 7:51:59 PM", "      9/9/2019 7:52:00 PM", "      9/9/2019 7:52:01 PM", 
    "      9/9/2019 7:52:02 PM", "      9/9/2019 7:52:03 PM", "      9/9/2019 7:54:00 PM", 
    "      9/9/2019 7:54:02 PM"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -11L))
  </code></pre>
  
  <p>I have tried the code below, which works well, except I am wanting the 7:51:59 and 7:52:00 to be in the same group. The only time the duration should break and create a new group, is when the time in between datetimes exceed 120 secs.</p>
  
  <pre><code>   df %&gt;%
     mutate(DATEB = lubridate::mdy_hms(DATEB), 
     temp = floor_date(DATEB, "120 secs")) %&gt;%
     group_by(temp) %&gt;%
     summarise(duration = difftime(max(DATEB), min(DATEB), units = "secs"))
  </code></pre>
  
  <p>Any suggestion is appreciated.</p>