R time format to display as in HH:MM:SS format

Hi all,
I have a dataframe with a time column having values like "0s" "300s (~5 minutes)" "600s (~10 minutes)" "900s (~15 minutes) "14400s (~4 hours)" "14700s (~4.08 hours)" "15000s (~4.17 hours)" "15300s (~4.25 hours)"etc. I would like to display it in hours minutes second format (eg:12H 05M 0S).

Any help would be highly appreciated.

Hi @sitaram , try to put an example of data for better get help.

See this guide:

This converts the character vector into Date objects, from which you can use format

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
the_times <- c("0s", "300s (~5 minutes)", 
            "600s (~10 minutes)", 
            "900s (~15 minutes)", 
            "14400s (~4 hours)",
            "14700s (~4.08 hours)",
            "15000s (~4.17 hours)",
            "15300s (~4.25 hours)")
begin <- Sys.Date()
(the_seconds <- as.numeric(gsub("s.*$","",the_times)))
#> [1]     0   300   600   900 14400 14700 15000 15300
full_datetimes <- (begin + seconds(the_seconds)) 
format(full_datetimes, format = "%H:%M:%S")
#> [1] "00:00:00" "00:05:00" "00:10:00" "00:15:00" "04:00:00" "04:05:00" "04:10:00"
#> [8] "04:15:00"

Created on 2023-03-15 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.