I have a continuous day with boat movements, that include several trips. I want to identify each trip with a different code (unique ID). Each boat trip can be detected because the period between successive points is larger. Note that the time is not regular.
For example:
library(dplyr)
rep_data <- data.frame(
t = c(1, 2, 3, 4,5,10, 12, 13,14,15,16, 23, 24,26,28),#this would be the time
expect_output = c(1, 1, 1, 1,1,2, 2, 2,2,2,2, 3, 3,3,3)) # this would be the unique ID of the trip
rep_data <- rep_data %>%
mutate(dif.time = c(t-lag(t,1)),
gp = ifelse(dif.time > 5, 1, 0))
I tried:
I tried with cumsum HERE
rep_data %>%
mutate(daynum = cumsum(!duplicated(gp)))
I tried with group_indices another one
rep_data %>%
group_by(dif.time) %>%
group_indices()
and also tried cur_group_id.
But I am not even close to solve this simple challenge.
The column expect_output indicates the result I wanted, that would be three boat trips during the complete period.
Any ideia how to get there? Any help will be greatly apretiated,
Thank you very much in advance,
Best regards, Marta