I'm trying to find the duration of overlapping time. I know I am close with my code, but am getting NA results when I try to use the intersect function. I'm hoping someone can help me see where I am going wrong.
I have followed much of the example here R Time periods overlapping - but am not quite getting it correct. I think I must be missing a detail....
library(data.table)
library(lubridate)
First I create 2 data tables with time periods to overlap
#create data table with observation shifts
start <- as.POSIXct(c("2017-08-01 04:52:00", "2017-08-01 05:56:00", "2017-08-01 13:25:00", "2017-08-01 13:50:00", "2017-08-01 14:29:00", "2017-08-01 15:28:00", "2017-08-01 15:57:00", "2017-08-01 17:22:00", "2017-08-01 17:37:00", "2017-08-01 19:10:00", "2017-08-01 22:52:00", "2017-08-02 04:00:00", "2017-08-02 05:02:00", "2017-08-02 08:47:00", "2017-08-02 13:31:00", "2017-08-02 18:13:00", "2017-08-03 04:31:00", "2017-08-03 05:34:00", "2017-08-03 07:20:00", "2017-08-03 08:25:00", "2017-08-03 09:33:00", "2017-08-03 12:20:00", "2017-08-03 14:04:00", "2017-08-03 21:21:00", "2017-08-07 18:22:00", "2017-08-08 10:20:00", "2017-08-08 14:05:00", "2017-08-08 15:57:00", "2017-08-08 17:01:00", "2017-08-09 04:27:00"), format = "%Y-%m-%d %H:%M:%S", tz = "America/Halifax")
end <- as.POSIXct(c("2017-08-01 05:03:00", "2017-08-01 12:22:00", "2017-08-01 13:31:00", "2017-08-01 13:59:00",
"2017-08-01 15:23:00", "2017-08-01 15:56:00", "2017-08-01 17:21:00", "2017-08-01 17:35:00","2017-08-01 19:08:00", "2017-08-01 22:51:00", "2017-08-02 00:00:00", "2017-08-02 04:59:00", "2017-08-02 08:41:00","2017-08-02 13:27:00", "2017-08-02 17:45:00", "2017-08-03 00:02:00", "2017-08-03 05:32:00", "2017-08-03 07:18:00", "2017-08-03 08:24:00", "2017-08-03 09:27:00", "2017-08-03 11:59:00", "2017-08-03 13:54:00", "2017-08-03 16:20:00", "2017-08-04 00:01:00",
"2017-08-07 20:03:00", "2017-08-08 14:04:00", "2017-08-08 14:40:00", "2017-08-08 16:57:00", "2017-08-09 00:02:00", "2017-08-09 05:09:00"), format = "%Y-%m-%d %H:%M:%S", tz = "America/Halifax")
effort <- data.table(start, end)
#create data table with daylight hours
day.start = seq.POSIXt(as.POSIXct("2017-07-31 05:30:00",
format = "%Y-%m-%d %H:%M:%S",
tz = "America/Halifax"),
as.POSIXct("2017-08-23 05:30:00",
format = "%Y-%m-%d %H:%M:%S",
tz = "America/Halifax"),by = "day")
day.end = seq.POSIXt(as.POSIXct("2017-07-31 20:00:00",
format = "%Y-%m-%d %H:%M:%S",
tz = "America/Halifax"),
as.POSIXct("2017-08-23 20:00:00",
format = "%Y-%m-%d %H:%M:%S",
tz = "America/Halifax"),by = "day")
daytimes = data.table(day.start, day.end)
Now to find where overlaps occur
#get ready to do foverlaps
setkey(effort, start, end)
setkey(daytimes, day.start, day.end)
#find overlapping periods
ov = foverlaps(daytimes,effort,type="any",nomatch=0)
# calculate intervals for effort daylight time periods that overlap
ov$int_eff = interval(ov$start, ov$end)
ov$int_day = interval(ov$day.start, ov$day.end)
# what time period is in common for each overlap?
Everything seems OK to this point. But when I try to find the intersection, I end up with a series of NA values
ov$int_o = intersect(ov$int_eff, ov$int_day)
# take a look to check
head(ov$int_o)
EDIT - including expected output here now.
After finding the intersects, I would then caulcate durations and then sum them up. For this example, the total duration is 46 hours.
# now calculate durations of intersect intervals
ov$dur = as.duration(ov$int_o)
round(sum(ov$dur/dhours(1), na.rm=T), 1)