Jack Jack - 3 months ago 14
R Question

taking time difference from time in different format in R

I have a data.table dt which have many records .

It have two columns datetime1, with value as "2017-04-19 09:54:00" of class

POSIXct


another column have time values like "7.97" of class
numeric
. It is for the same date.

I want to calculate a difference in the time in minutes. how can I do it in R

d.b d.b
Answer Source

You can use functions of lubridate to extract hour, minute, and second of the POSIXct and then calculate the difference.

library(lubridate)
x = as.POSIXct("2017-04-19 09:54:00", tz = "UTC")
hour(x) * 60 + minute(x) + second(x)/60 - 7.97 * 60
#[1] 115.8