Reputation: 323
Say I have a start date and time "2016-01-09 15:23:26" and measurements taken at varying intervals with the date and time recorded for each measurement.
Sample data follow:
date time date_time clock
"2016-01-09" "15:23:26" 2016-01-09 15:23:2 0s (0 seconds)
"2016-01-10" "23:59:53" 2016-01-09 23:59:53 30987s (~8.61 hours)
"2016-01-10" "00:04:53" NA NA
"2016-01-10" "01:04:55" 2016-01-10 01:04:55 34889s (~9.69 hours)
If I try to use lubridate
to calculate the duration, in minutes since the start time, the calculation works fine for all but row 3 (where the date is "2016-01-10" and the time is "00:04:53". I've tried to look on the web for some possible explanation for what is going wrong here but to no avail. What am I missing? My code follows:
library(lubridate)
df$date2 = mdy(df$Date)
df$time2 = hms(df$Time)
df$date_time = paste(df$date2, df$time2, sep = " ")
df$date_time = ymd_hms(df$date_time)
df$start_time = ymd_hms("2016-01-09 15:23:26", tz="UTC")
df$clock = as.duration(df$date_time - df$start_time)
Upvotes: 1
Views: 923
Reputation: 19544
When you paste date and time, because third time is less than an hour, it is not recognized by ymd_hms()
Since you already have POSIXct objects, you can sum them directly.
Simply replace :
df$date_time = paste(df$date2, df$time2, sep = " ")
df$date_time = ymd_hms(df$date_time)
by:
df$date_time = df$date2+df$time2
Upvotes: 1