I have been given a table of data to analyse. I have a column with start times and a column with finish times given in 3 or 4 digits, example: 3:40pm is 1540, 7:25am is 725.
How can I obtain a new column in my table with the difference of these times, given in minutes? There are over 2000 entries.
Thanks for any help.
structure(list(schedtime = c(1455L, 1640L, 1245L, 1715L, 1039L,
840L), deptime = c(1455L, 1640L, 1245L, 1709L, 1035L, 839L),
distance = c(184L, 213L, 229L, 229L, 229L, 228L), flightnumber =
c(5935L,
6155L, 7208L, 7215L, 7792L, 7800L), weather = c(0L, 0L, 0L,
0L, 0L, 0L), dayweek = c(4L, 4L, 4L, 4L, 4L, 4L), daymonth = c(1L,
1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 6L), class = "data.frame")
This is an example of the dataset (Im unsure are to why there are L's after each number, these are not shown in the table). I want, in minutes, deptime (finish) - schedtime (start).
Given the new column, there are 2 values with a schedtime before midnight and a deptime after midnight, for example schedtime 2120 and deptime 0010. The answer to this is given as -1270, considering it to be an extremely early departure. How could I change this to be calculated as +170, a late departure?
Efficient way for larger dataset-
EDIT- (To handle cases like 1730 - 1600 = 130 mins ( Actually, it is 90 mins).
Taking the floor of the the hundreds gives the hours. Taking the difference and multiplying by 60 gives the minutes from the difference of the hours. Then you can subtract the original minutes and add the final minutes to get total minutes passed.
You can use library lubridate to find the difference in minutes . Hope this helps. lubridate provides very good functionality for time related data.
Data
I have the same query, Is there a way to calculate the time difference of times in a column and display the answers in a new column in minutes