I have two character strings that describe time of day.
session_start <- "9:31:56"
session_end <- "10:57:25"
I essentially want to subtract session_start from session_end to get the time difference (or time elapsed to put it another way). How can I do that?
I’d finally like to convert the result of session_end - session_start from an H:M:S output to only total minutes. Thanks
>Solution :
a <- diff(as.POSIXct(paste(Sys.Date(), c(session_start, session_end))))
as.numeric(a, units = 'mins')
[1] 85.48333
using data.table:
(b <- diff(data.table::as.ITime(c(session_start,session_end))))
[1] "01:25:29"
To convert this to minutes use:
as.numeric(structure(b, class='difftime', units='secs'), units='mins')
[1] 85.48333
Using basic math:
d <- read.table(text=c(session_start,session_end), sep = ':')
diff(c(c(60, 1, 1/60) %*% t(d)))
[1] 85.48333