Home > database >  How can I parse string values to a workable POSIXct format when the duration of some of the values a
How can I parse string values to a workable POSIXct format when the duration of some of the values a

Time:02-05

Please see this previous question I asked.

I have a data set in R that has values in hours, minutes, and seconds format. However, some values only have hours and minutes, some only have minutes and seconds, some only have minutes, and some only have seconds. It's also not formatted very favorably. When I use parse_date_time() on it, the parsing only works for values that are less than 24 hours. Sample data can be found below:

example <- as.data.frame(c("25h28m", "17m7s", "15m", "14s"))

Using parse_date_time(), I get this:

Column Title
NA
00:17:07
00:15:00
00:00:14

But would like to get this:

Column Title
25:28:00
00:17:07
00:15:00
00:00:14

Any help would be greatly appreciated. Thanks!!

CodePudding user response:

We may do this by converting to period class from lubridate and then use hms from hms

library(lubridate)
hms::hms(as.period(toupper(example)))

-output

25:28:00
00:17:07
00:15:00
00:00:14

data

example <- c("25h28m", "17m7s", "15m", "14s")
  •  Tags:  
  • Related