我正在尝试查找重叠时间的持续时间。我知道我与我的代码很接近,但是当我尝试使用相交函数时却得到了NA结果。我希望有人能帮助我看看我要去哪里错了。
我在这里R Time periods overlapping中遵循了很多示例-但并不太正确。我想我一定错过了一个细节。...
library(data.table)
library(lubridate)
首先,我创建2个数据表,这些数据表的时间段要重叠
#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)
现在查找重叠的地方
#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?
到目前为止,一切似乎都还可以。但是当我尝试找到交点时,我最终得到了一系列NA值
ov$int_o = intersect(ov$int_eff, ov$int_day)
# take a look to check
head(ov$int_o)
编辑-现在包括此处的预期输出。
找到相交后,我将计算持续时间,然后将其汇总。对于此示例,总持续时间为46小时。
# now calculate durations of intersect intervals
ov$dur = as.duration(ov$int_o)
round(sum(ov$dur/dhours(1), na.rm=T), 1)