How can I convert this string column to duration per min. I intent to use wildcard character but I have some problems when convert this
Here is one method.
DF <- data.frame(Duration = c("24 min per ep",
"1 hr 55 min",
"24 min per ep",
"24 min"))
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.3
DF |> mutate(Hour = str_extract(Duration, "^\\d+(?= hr)"),
Hour = ifelse(is.na(Hour), 0, Hour),
Min = str_extract(Duration, "\\d+(?= min)"),
Duration2 = as.numeric(Hour) * 60 + as.numeric(Min))
#> Duration Hour Min Duration2
#> 1 24 min per ep 0 24 24
#> 2 1 hr 55 min 1 55 115
#> 3 24 min per ep 0 24 24
#> 4 24 min 0 24 24
Created on 2024-06-09 with reprex v2.0.2
2 Likes
Thanks for your help <3
I'm guessing @FJCC 's solution is exactly what you were looking for — a way to extract the number of hours and minutes from a string — but in case you wanted to create a column whose data type is duration
, the lubridate package's duration()
function can convert strings similar to yours into durations:
library(lubridate)
duration("1 hour 55 min")
#> [1] "6900s (~1.92 hours)"
Created on 2024-06-10 with reprex v2.0.2
3 Likes
This topic was automatically closed 7 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.