My data is currently in two separate columns
T1 example looks like 2020-05-01 09:00:00 PM
T2 example took like 2020-05-02 04:00:00 AM
This data also contains NAs
Data mode and class is characters
I would like to know the difference between these in hours.
I tried a function like so
df$TimeElapsedhours <- NA
for (i in 1:nrow(df)) {
if (!is.na(df$T1[i])) {
df$TimeElapsedDays[i] <- difftime(df$T2[i], df$T1[i], units = c("hours"))
}
}
but the values dont seem right it is giving me absurd answers higher than what is right something is going on.
I tried something like this
df$DIF <- as.numeric(difftime(strptime(df$T1 , "%I:%M %p" ),strptime(df$T2 , "%I:%M %p" ),units='hours'))
and it returns all NAs
So... how can i find the time difference between two time points with data that includes NAs
We don't need a
forloop here, we can uselubricateto parse the date time, andmutatefromdplyr.This gives us
With NAs this will give an NA in the TimeElapsedDays col as below
But we can remove that