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