Search code examples
rdatetimeroundinglubridatedatetime-conversion

How can I round seconds to 2 places upon using R package lubridate's seconds_to_period function?


I converted seconds to DD:HH:MM:SS format using the R package Lubridate's function seconds_to_period. Now I have a huge list of times which look like this -> 2d 7H 1M 5.31500000000233S. I need to round off the seconds to 2 decimal places.
I tried using round_date and ymd_hms but I receive this error in as.POSIXct.numeric(x) : 'origin' must be supplied. I tried if I could access the seconds separately and write a function to change them but could not. How can I solve this?

Attached a duration.RData file and durdata.R file for reference data. Both the files have the same data but one is using dput() and other using save(). Link : link to files


Solution

  • We can use round to format decimal places in "Period" objects.

    head(round(time, 2))
    #[1] "2d 7H 1M 5.32S"   "1d 9H 20M 37.49S" "7H 39M 37.95S" "2d 5H 0M 41.01S" 
    #    "1d 8H 32M 2.8S"   "6H 36M 25.19S"