Search code examples
rdifftime

as.difftime() from string


The result of the following instruction:

as.difftime("5 11:04:36", "%d %H:%M:%S", units =("mins"))

is

Time difference of -7975.4 mins

It seems that this function is calculating the time difference between Sys.Time() and the given value. I actually need an object to store a time span value extracted from a string (). Am I using the wrong function or is it not the right way of using it?


Solution

  • Look into lubridate

    I think you want something like Duration-class, Period-class, or Timespan-class

    # Duration
    dur <- duration(hours = 10, minutes = 6)
    # [1] "36360s (~10.1 hours)"
    
    # Period
    per <- period(hours = 10, minutes = 6)
    # [1] "10H 6M 0S"