Home > Software design >  Converting format %H %M %S (no leading 0s) to HH:MM:SS in r
Converting format %H %M %S (no leading 0s) to HH:MM:SS in r

Time:09-05

I realize this may be a very basic question, but I am under a time crunch and cannot for the life of me figure this out. Someone altered this data I need to use to go from HH:MM:SS to %%H %%M %%S without leading 0s when these values are not double digits (so maybe more like (%)%H (%)%M (%)%S if that makes sense). For example, 10:32:20 would be represented as 10H 32M 20S. I can deal with this just fine. However, if it is 7:30:04, for example, it will be 7H 30M 4S. I don't understand why someone would use this format - is this a standard in some package?

I am looking for a way to convert this to to the HH:MM:SS format / parse it into a time object.

I initially only realized that the seconds were missing leading 0s, so that was what this formula was for..

# turn e.g. 10H 12M 30S -> 10:12:30
df$Time = gsub(" ", "", c(df$Time))
df$Time = gsub("[^0-9]", ":", c(df$Time))
df$Time = gsub("::", ":", c(df$Time))
df$Time = substring(df$Time,1,nchar(df$Time) - 1)

 for (i in seq_along(df$Time)) {
   if (nchar(df$Time[i]) == 7) {
     gsub("([0-9]*$)", paste(0, str_sub(df$Time[i], start=7), sep=""), df$Time[i])
   }
}

CodePudding user response:

These look like "period" objects from the lubridate package. If you want times in the HH:MM:SS format, you can define a little converter function:

library(lubridate)

f <- function(x) substr(as.POSIXct("2022-01-01", tz = "GMT")   hms(x), 12, 19)

So, for example:

times <- c("10H 12M 30S", "9H 53M 22S")

f(times)
#> [1] "10:12:30" "09:53:22"

Created on 2022-09-04 with reprex v2.0.2

CodePudding user response:

Actually, it is not even necessary to load a package.

x
# [1] "1H 33M 4S"   "3H 7M 5S"    "4H 3M 51S"   "4H 23M 22S" 
# [5] "4H 39M 33S"  "5H 19M 27S"  "5H 21M 2S"   "5H 29M 48S" 
# [9] "7H 41M 23S"  "9H 35M 1S"   "10H 27M 3S"  "13H 17M 53S"
# [13] "14H 42M 52S" "15H 32M 34S" "15H 57M 5S"  "17H 4M 44S" 
# [17] "17H 57M 49S" "18H 2M 55S"  "18H 21M 49S" "20H 14M 25S"

strptime(paste(Sys.Date(), x), '%F %HH %MM %SS') |>
  strftime('%R:%S')
# [1] "01:33:04" "03:07:05" "04:03:51" "04:23:22" "04:39:33"
# [6] "05:19:27" "05:21:02" "05:29:48" "07:41:23" "09:35:01"
# [11] "10:27:03" "13:17:53" "14:42:52" "15:32:34" "15:57:05"
# [16] "17:04:44" "17:57:49" "18:02:55" "18:21:49" "20:14:25"

Data:

x <- c("1H 33M 4S", "3H 7M 5S", "4H 3M 51S", "4H 23M 22S", "4H 39M 33S", 
"5H 19M 27S", "5H 21M 2S", "5H 29M 48S", "7H 41M 23S", "9H 35M 1S", 
"10H 27M 3S", "13H 17M 53S", "14H 42M 52S", "15H 32M 34S", "15H 57M 5S", 
"17H 4M 44S", "17H 57M 49S", "18H 2M 55S", "18H 21M 49S", "20H 14M 25S"
)

CodePudding user response:

And here is another way.

convert_time <- function(x) {
  y <- gsub("[[:alpha:]] ", ":", x)
  y <- as.POSIXct(paste(Sys.Date(), y))
  sub("^[^ ]  ", "", y)
}

x <- c("10H 32M 20S", "7H 30M 4S", "10H 12M 30S")
convert_time(x)
#> [1] "10:32:20" "07:30:04" "10:12:30"

Created on 2022-09-04 by the reprex package (v2.0.1)

  • Related