I have a dataframe containing an animal ID and a timestamp (this is simplified GPS data). The df is ordered by date/time. I want to create a column that determines the trip number. Trips are split if the interval between one time and the next is >28800 seconds.
#some sample data
timestamp <- as.POSIXct(c("18/01/2020 06:43:38", "18/01/2020 06:44:14", "18/01/2020 16:45:07" ,"18/01/2020 16:46:07"), tz = "UTC", format = "%d/%m/%Y %H:%M:%S")
data <- data.frame("ID" = c("a","b","c","d"), "timestamp" = timestamp)
#ORIGINAL DATAFRAME
# ID timestamp
#1 a 2020-01-18 06:43:38
#2 b 2020-01-18 06:44:14
#3 c 2020-01-18 16:45:07
#4 d 2020-01-18 16:46:07
data$interval <- data$timestamp - lag(data$timestamp, n = 1L) #calculates time difference between points
data$trip <- c(1,1,2,2) # THIS IS THE LINE I NEED HELP WITH
#DATAFRAME I WANT IN THE END
#ID timestamp interval trip
#1 a 2020-01-18 06:43:38 NA secs 1
#2 b 2020-01-18 06:44:14 36 secs 1
#3 c 2020-01-18 16:45:07 36053 secs 2
#4 d 2020-01-18 16:46:07 60 secs 2
I would also be ok with subsetting the data (see example below).
$`1`
ID timestamp interval
1 a 2020-01-18 06:43:38 NA secs
2 b 2020-01-18 06:44:14 36 secs
$`2`
ID timestamp interval
3 c 2020-01-18 16:45:07 36053 secs
4 d 2020-01-18 16:46:07 60 secs
I am struggling to explain myself, I hope this makes sense!