I have this kind of .csv (trip, distance and time):
I would like to calculate the duration of each trip in R.
P.S.: The main problem could be the format of the time. Thank you in advance
I have this kind of .csv (trip, distance and time):
I would like to calculate the duration of each trip in R.
P.S.: The main problem could be the format of the time. Thank you in advance
Here is one solution. For each trip you have duration:
library(dplyr)
library(lubridate)
df <- tibble(Trip= c(34,34,34,35,35,36, 36, 36, 36),
Distance = seq(1,9),
Time= c("17:30", "18:00", "18:30", "17:30","18:00", "17:30", "18:30","19:00", "19:30"))
df %>% mutate(Time = hm(Time)) %>%
group_by(Trip) %>%
mutate(Trip_time = duration(as.double(tail(Time, n=1)-head(Time, n=1))))
PS: your question is poorly written.