Split a column and convert time from ( H:M:S and M:S) format in seconds

The code below, with time formatted in such a way, runs perfectly to my aim:
s1 = c("PT57M3S", "PT1H3M46S","PT1H33S","PT1H2M", "PT18S","PT18M9S", "PT39M22S")
library(tidyverse)
library(stringi)
time <- map2_df(stri_extract_all_regex(s1, "\d+"),
stri_extract_all_regex(s1, "[HMS]"), ~ .x %>%
as.integer %>%
as.list %>%
set_names(.y) )
time[is.na(time)] <- 0
elapsed_time <- time[3] *3600 + time[1] *60 + time[2]

I GET THE RESULT in seconds

Here I have this column extracted from a data.table:
"1:07:38" "48:14.02" "38:01.68" "53:22.97" "45:23.52"
#Convert it to a data.frame:
time_format <- as.data.table(res_df$elapsed_time)
#After running the function map2_df I get this error and warning:
Error in dplyr::bind_rows():
! Argument 1 must be a data frame or a named atomic vector.
Run rlang::last_trace() to see where the error occurred.
Warning messages:
1: In stri_extract_all_regex(time_format, "\d+") :
argument is not an atomic vector; coercing
2: In stri_extract_all_regex(time_format, "[HMS]") :
argument is not an atomic vector; coercing

I'm sure time_format is a data.frame with the time results:
V1
1: 1:07:38
2: 48:14.02
3: 38:01.68
4: 53:22.97
5: 45:23.52

The real difference between the s1 vector and the second I used is the presence of column ":" in time.
Is this the issue?
I want to precise that some times are H:M:S others M:S, that make the code harder to resolve.
I hope someone can help me.

Thanks in advance,
FS

I would do the conversion by filling in the missing hours value and using the hms package to convert the character values to time values. In the example, I made three new columns but you can simple replace the V1 column if you want to.

library(tidyverse)
library(stringi)
library(hms)
#> 
#> Attaching package: 'hms'
#> The following object is masked from 'package:lubridate':
#> 
#>     hms
time_format <- data.frame(V1 = c("1:07:38", "48:14.02", "38:01.68", 
                                 "53:22.97", "45:23.52"))
time_format
#>         V1
#> 1  1:07:38
#> 2 48:14.02
#> 3 38:01.68
#> 4 53:22.97
#> 5 45:23.52
time_format <- time_format |> 
  mutate(V1_filled = ifelse(stri_count(V1, fixed = ":") == 1, 
                     paste("0", V1, sep = ":"), V1),
         V1_hms = as_hms(V1_filled),
         V1_s = as.numeric(V1_hms))
time_format
#>         V1  V1_filled      V1_hms    V1_s
#> 1  1:07:38    1:07:38 01:07:38.00 4058.00
#> 2 48:14.02 0:48:14.02 00:48:14.02 2894.02
#> 3 38:01.68 0:38:01.68 00:38:01.68 2281.68
#> 4 53:22.97 0:53:22.97 00:53:22.97 3202.97
#> 5 45:23.52 0:45:23.52 00:45:23.52 2723.52

Created on 2023-11-27 with reprex v2.0.2

Thanks a lot for this suggestion. It works well.
FS

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.