rposixctchron

Converting Excel times to POSIXct in r


I'm using an Excel dataset where the time values, MM:SS, come in numeric values that I need to convert to POSIXct in r and then make calculations.

Below is sample data of what I have and I need to get

dfOrig <- data.frame(StandarTime =  c(615,735,615 ),
                 AchievedTime =  c(794,423,544 ))

This is what I'm looking for:

dfCleaned <- data.frame(StandarTime =  c("2017-08-25 10:15",
                                     "2017-08-25 12:15",
                                     "2017-08-25 10:15" ),
                    AchievedTime =  c("2017-08-25 13:14 PDT",
                                      "2017-08-25 7:03 PDT",
                                      "2017-08-25 9:04 PDT" ))

I'm not sure how to best approach this problem.


Solution

  • Not sure what the values are but in case these are seconds you can use:

    > dfOrig$StandarTime <- ISOdate(2017, 8, 25, hour = 0) + dfOrig$StandarTime
    > dfOrig$AchievedTime <- ISOdate(2017, 8, 25, hour = 0) + dfOrig$AchievedTime
    > dfOrig
              StandarTime        AchievedTime
    1 2017-08-25 00:10:15 2017-08-25 00:13:14
    2 2017-08-25 00:12:15 2017-08-25 00:07:03
    3 2017-08-25 00:10:15 2017-08-25 00:09:04
    

    ISOdate(2017, 8, 25, hour = 0) sets the start time, then you can add a value in seconds. You can also specify a time zone using tz = ""