Subtracting time duration from a timestamp in R

I have a data frame as follows with a Session end time (%D/%M/%Y %H:%M:%S) and Session Duration (%M:%S). I want to obtain the Session start time in the format %D/%M/%Y %H:%M, by subtracting the Session Duration from the Session End time and attaching it to the data frame as a separate column called "SessionEnd".
How am I supposed to do this in R?

SessionEnd <- c("22/06/2022 0:01:00", "22/06/2022 0:09:00", "22/06/2022 0:10:00", "22/06/2022 0:11:00", "22/06/2022 0:14:00")

Session_Duration <- c("6:11", "8:22", "9:59", "8:09", "4:55")

df <- data.frame(SessionEnd, Session_Duration)

The output should look like the one below.

Session_End Session_Duration Session_Start
22/06/2022 0:01:00 6:11 21/06/2022 23:54:49
22/06/2022 0:09:00 8:22 22/06/2022 00:00:38
22/06/2022 0:10:00 9:59 22/06/2022 00:00:01
22/06/2022 0:11:00 8:09 22/06/2022 00:02:51
22/06/2022 0:14:00 4:55 22/06/2022 00:09:05

>Solution :

It would be easy to do this with functions from lubridate package –

library(dplyr)
library(lubridate)

df %>%
  mutate(SessionEnd = dmy_hms(SessionEnd), 
         Session_Duration = ms(Session_Duration), 
         SessionStart = SessionEnd - Session_Duration) #%>%
  #If you want to maintain the same format as input add the below line
  #mutate(across(c(SessionEnd, SessionStart), format, '%d/%m/%Y %H:%M:%S'))

#           SessionEnd Session_Duration        SessionStart
#1 2022-06-22 00:01:00           6M 11S 2022-06-21 23:54:49
#2 2022-06-22 00:09:00           8M 22S 2022-06-22 00:00:38
#3 2022-06-22 00:10:00           9M 59S 2022-06-22 00:00:01
#4 2022-06-22 00:11:00            8M 9S 2022-06-22 00:02:51
#5 2022-06-22 00:14:00           4M 55S 2022-06-22 00:09:05

Leave a Reply