0

Dear R community members, i would like to create a new variable (commute time) based on the difference between the departure and arrival time of commuters (Arrivaltime - Departuretime) from the Origin to their destination (24 hour format).

However, the problem is that the column is numeric and the values more than 30 minutes are not captured as minutes. I want my commutetime be in minutes not in hours. Below is the format of my dataset.

Departuretime  Arrivaltime commutetime 
950            1000        50
1030           1035        5
1750           1800        50
1520           1530        10

When i take the difference between the departure and arrival time, things get weird beyond 30 minutes which you of course would expect to happen. My dataframe has got 6,670 entries and these columns are the only problematic thing. The difference between 950 and 1000 should translate into the difference for 9:50 and 10:00 and therefore the difference can't be 50. As illustrated in the second last row, the difference between 1750 and 1800 should not yield 50.

I would immensely appreciate your timely help.

Thank you!!!

Xaviermoros
  • 131
  • 10
  • Does this answer your question? [R convert number into time](https://stackoverflow.com/questions/31167460/r-convert-number-into-time) – Björn Apr 16 '20 at 07:55
  • See also [convert-numbers-to-time-in-r](https://stackoverflow.com/questions/52165284/convert-numbers-to-time-in-r) – Björn Apr 16 '20 at 07:56

1 Answers1

0

Convert Arrivaltime and Departuretime in POSIXct format and use difftime to calculate difference in minutes.

df$commutetime <- difftime(
               as.POSIXct(sprintf("%04d", df$Arrivaltime), format = "%H%M"), 
               as.POSIXct(sprintf("%04d", df$Departuretime), format = "%H%M"), 
               units = "mins")

df
#  Departuretime Arrivaltime commutetime
#1           950        1000     10 mins
#2          1030        1035      5 mins
#3          1750        1800     10 mins
#4          1520        1530     10 mins

data

df <- structure(list(Departuretime = c(950L, 1030L, 1750L, 1520L), 
Arrivaltime = c(1000L, 1035L, 1800L, 1530L)), row.names = c(NA, 
-4L), class = "data.frame")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Dear Ronak, Excellent it has worked. Do i need to consider the "data" subtitle as well? It seems the first code has sorted out my problem. I greatly appreciate your timely help. – Xaviermoros Apr 16 '20 at 08:07
  • @AbdirashidM.Dahir No, you can ignore that part it is a way to share reproducible data. The first part is the answer. – Ronak Shah Apr 16 '20 at 08:32
  • I have marked it "useful" Thanks a lot Ronak. But when i have tried to use a ggplot for the mode of travel (car, bus, bicycle) -- which is character (factor) on x axis --- against commutetime on y axis via the following code, i get weird warning and a graph with negative values (impossible). Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.Below is my code. ggplot(jongnocommutetime, aes(x = mode , y = commutetime)) + + theme_bw()+ + geom_col()+ + labs (y= "Travel mode", + title = "Mode share by trip purpose") – Xaviermoros Apr 16 '20 at 09:27
  • I am trying but i get this reply "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Xaviermoros Apr 16 '20 at 14:04
  • Accepted and i have got another problem. Check out my upcoming question in the next 10 minutes. I would greatly appreciate your help!!! – Xaviermoros Apr 16 '20 at 14:07
  • Thanks regarding your `ggplot` attempt you should probably ask a new question but you might need to convert `commutetime` column to numeric. `df$commutetime <- as.numeric(df$commutetime)`. – Ronak Shah Apr 16 '20 at 14:11
  • Thank you very much!!! i have already posted that question on my world. Your help is indispensable! – Xaviermoros Apr 16 '20 at 14:25