С dplyr
:
df %>%
mutate(ArrivalTime = paste0(arrivalhour,":",arrivalminute),
DepartTime = paste0(departurehour,":",departureminute)) %>%
select(ends_with("Time")) %>%
mutate(DepartTime = strptime(DepartTime, format="%H:%M"),
ArrivalTime = strptime(ArrivalTime, format="%H:%M"),
Total =difftime(ArrivalTime, DepartTime))
ArrivalTime DepartTime Total
1 2020-04-16 04:50:00 2020-04-16 04:30:00 20 mins
2 2020-04-16 09:30:00 2020-04-16 09:10:00 20 mins
3 2020-04-16 08:18:00 2020-04-16 08:10:00 8 mins
ПРИМЕЧАНИЕ Для работы difftime
необходим некоторый компонент даты.
Данные
df <- structure(list(departurehour = c(4L, 9L, 8L), departureminute = c(30L,
10L, 10L), arrivalhour = c(4L, 9L, 8L), arrivalminute = c(50L,
30L, 18L)), class = "data.frame", row.names = c(NA, -3L))