-3

I have this kind of .csv (trip, distance and time):

enter image description here

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

Jan Rothkegel
  • 737
  • 3
  • 21
Mauri21
  • 11
  • 3
  • Provide a reproducible example so it is easier to help https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Albin Jan 13 '22 at 11:06
  • Input data: Trip Distance Time 34 1.8 17:30 34 1.5 18:00 34 3.0 18:30 34 5.1 19:00 35 3.2 15:32 35 1.1 18:43 36 2.3 16:20 36 3.3 16:25 36 4.0 20:30 Output data: Trip Time 34 1:30 35 3:11 36 4:10 – Mauri21 Jan 13 '22 at 11:20
  • Albin pointed you to the suggestions about providing reproducible examples. Did you read it? – Claudio Jan 13 '22 at 12:17

1 Answers1

0

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.

Bloxx
  • 1,495
  • 1
  • 9
  • 21