Home > OS >  How Can I Convert a String to Hours, Minutes, and Seconds in R?
How Can I Convert a String to Hours, Minutes, and Seconds in R?

Time:04-03

I want to change the values in a time variable so from a string to time. I have looked up how to do this and have not found results. I have not been able to find a solution for a string formatted in this way:

ex: 57M 13S ex: 34S ex: 54H 3M 23S

I would like it to be formatted as:

ex: 57:13:00 ex: 00:00:34 ex: 54:03:23

What can be done so that I can change the formatting in this way?

CodePudding user response:

Assuming the first element is minutes, seconds, convert the string to period class and use hms

library(period)
hms::hms(as.period(str1))
00:57:13
00:00:34
54:03:23

data

str1 <- c("57M 13S", "34S", "54H 3M 23S")
  • Related