Calculating duration in a vector

Hello!
I have a data frame wherein one column has the start and end times of specific sessions. Each row in that column has an input like this:
"2020-12-21 09:00:00 -0600/2020-12-21 09:30:00 -0600"

Is there a function that can convert the values of the column into a duration? Like "0.5 hours" for the example above instead of all of that extra information.

Thank you for your time and assistance!

library(tidyverse)
library(lubridate)

(df1 <- tibble(a="a",
           b="2020-12-21 09:00:00 -0600/2020-12-21 09:30:00 -0600"))
(df2 <- df1 %>% mutate(splt = str_split(value1,"/",simplify = FALSE)))
# i ignore the timezone as it doesnt seem relevant to you (unless you have mixed timezone data ??)
(df3 <- df2 %>% mutate(pdt1 = parse_datetime(str_sub(splt[[1]][1],start = 1,end = -7)),
                       pdt2 = parse_datetime(str_sub(splt[[1]][2],start = 1,end = -7))))
(df4 <- df3 %>% mutate(intrvl = as.duration(lubridate::interval(start=pdt1,end = pdt2))))

select(df4,
       intrvl)
1 Like

Thank you so so much!!!

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.