I have a dataset, df: (the dataset contains over 4000 rows)
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
I wish to place in groups (if the times are not within 10 seconds of the previous row) and then take the duration of the newly formed group.
Desired output:
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))
I have tried the code below, which works well, except, the times seems to cut off right before 2 min, not giving me an accurate duration. (some of the datetimes exceed 2 min)
library(dplyr)
df %>%
mutate(DATEB = lubridate::mdy_hms(DATEB),
temp = cut(DATEB, breaks = "2 mins")) %>%
group_by(temp) %>%
summarise(duration = difftime(max(DATEB), min(DATEB), units = "secs"))
Any suggestion is appreciated.