Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: How can I convert non-intuitive strings that describe hours, minutes, and seconds to a workable POSIXct format to perform standard arithmatic on?

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. Sample data can be found below:

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

I’d like to convert it to a POSIXct format like the below, with the goal of adding/subtracting time:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I’ve tried as POSIXct() and strptime() formulas, but to no avail. Any help would be greatly appreciated – thanks!

>Solution :

Maybe parse_date_time from lubridate?

library("lubridate")
x <- c("22h28m", "17m7s", "15m", "14s")
y <- parse_date_time(x, orders = c("%Hh%Mm%Ss", "%Hh%Mm", "%Hh%Ss", "%Mm%Ss", "%Hh", "%Mm", "%Ss"), exact = TRUE)
y
## [1] "0000-01-01 22:28:00 UTC"
## [2] "0000-01-01 00:17:07 UTC"
## [3] "0000-01-01 00:15:00 UTC"
## [4] "0000-01-01 00:00:14 UTC"

To get a number of seconds since midnight, you could do:

y0 <- ymd_hms("0000-01-01 00:00:00")
dy <- y - y0
dy
## Time differences in secs
## [1] 80880  1027   900    14

Then you could add dy to any reference time given as a length-1 POSIXct object. For example, the most recent midnight:

y0 <- floor_date(now(), unit = "day")
y0 + dy
## [1] "2022-02-03 22:28:00 EST"
## [2] "2022-02-03 00:17:07 EST"
## [3] "2022-02-03 00:15:00 EST"
## [4] "2022-02-03 00:00:14 EST"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading