Vine
Vine

Reputation: 395

R - How to convert decimal to MM:SS.XXX

How do you convert SS.xxx (Seconds.Milliseconds) to MM:SS.xxx (Minutes:Seconds.Milliseconds) using R?

For example, my input is

time = 92.180

my desired output is

time = 01:32.180

All time fields have 3 decimal places.

Upvotes: 0

Views: 982

Answers (1)

DPH
DPH

Reputation: 4344

one option is the lubridate package - since you did not specify the output class I included a few possible outputs:

package(lubridate)

t <- 92.180
# your output string as character
lubridate::seconds(t) %>% 
  lubridate::as_datetime() %>% 
  format("%M:%OS3") 

# output as period
lubridate::seconds(t) %>% 
  lubridate::as.period()

# output as duration
lubridate::seconds(t) %>% 
  lubridate::as.duration() 

# output as time time
lubridate::seconds(t) %>% 
  lubridate::as.difftime()

Upvotes: 2

Related Questions