1

I have some text time fields in HHMM format and I need to convert the DIFFERNCET between the text times to a decimal value. Below are the 2 text time columns "TextStartTime" and "TextEndTime" and a column called "Difference in Text Time" that is the difference between

TextStartTime    Text end time      Diff. in Text Time     Decimal TIME in Hours
    0930             1300                  370                               3.5 

    1015             1700                  685                                6.45  

    930              1600                  670                                6.5   

    0945             1725                  780                                 7.66 

    0930             1540                  610                                  6.16

    1000             1645                   645                                 6.75

I know this is not an R problem but what algorithm can I use to convert the "Difference in Text Time" field to the "Decimal Time in hours field"?

I started by saying if the minutes of the "Difference in Text Time" column are >30 then subtract 40 from the minutes so the "670" would be "630" and then I can convert the 630 to 6.5 BUT that does not work for the "645" when I subtract 40 I get

user3022875
  • 8,598
  • 26
  • 103
  • 167

1 Answers1

2

I am assuming your times are numeric (although this might not be the case, because you have leading zeros. If they are strings, just use as.numeric() first).

"diff" gives what you have in your "Diff. in Text Time" column. Divide that by 100 to get the number of hours. The unadjusted number of minutes is what's left if you subtract the hours. For those minutes that are greater than 60, subtract 40. Then get what fraction of an hour they are. Add hours and minutes for decimal time.

TextStartTime <- c(930, 1015, 930, 0945, 0930, 1000)
Textendtime <- c(1300, 1700, 1600, 1725, 1540, 1645)

diff <- Textendtime-TextStartTime
hours <- floor(diff/100)
mins <- (diff/100-floor(diff/100))*100
mins[mins>60] <- mins[mins>60]-40
mins <- mins*1/60
time <- hours+mins
Molly OW
  • 113
  • 8