Q:

Converting numeric column (Difference between arrival and departure time ) of dataframe to minutes

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!!!

A:

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")

