r - 在 data.table 的区间内按日期选择行
问题描述
我想在数据表中选择属于第二个数据表中指定的时间间隔内的观察值 - 间隔是同时从 2 个平台进行观察的时间段。
第一个数据表看起来像这样。这是一堆动物目击事件。
obs = data.table(sighting = as.POSIXct(c("2018-08-12 16:30:00", "2018-08-12 16:35:00", "2018-08-12 16:38:00", "2107-08-13 15:13:00", "2107-08-13 16:13:00", "2017-08-14 11:12:13"), format = "%Y-%m-%d %H:%M:%OS", tz = "America/Halifax"), encounter = c("1", "1", "1", "2", "3", "4"), what = c("frog", "frog", "toad", "bird", "goat","bird"))
从 2 个平台进行观察。
platformA = data.table(station = "A", on.effort = as.POSIXct(c("2018-08-12 16:00:00", "2018-08-12 17:35:00","2017-08-14 11:00:13", "2018-08-15 17:35:00"), format = "%Y-%m-%d %H:%M:%OS", tz = "America/Halifax"), off.effort = as.POSIXct(c("2018-08-12 16:36:00", "2018-08-12 18:35:00","2017-08-14 12:12:13", "2018-08-15 18:35:00"), format = "%Y-%m-%d %H:%M:%OS", tz = "America/Halifax"))
platformB = data.table(station = "B", on.effort = as.POSIXct(c("2018-08-12 16:15:00", "2018-08-12 17:40:00", "2018-08-13 17:40:00","2017-08-14 11:05:13"), format = "%Y-%m-%d %H:%M:%OS", tz = "America/Halifax"), off.effort = as.POSIXct(c("2018-08-12 16:40:00", "2018-08-13 17:45:00", "2018-08-12 18:20:00","2017-08-14 12:30:13"), format = "%Y-%m-%d %H:%M:%OS", tz = "America/Halifax"))
我首先计算了每个平台的间隔,然后将这些间隔相交以找出同时进行观察的时间。
setkey(platformA, on.effort, off.effort)
setkey(platformB, on.effort, off.effort)
common = foverlaps(platformA, platformB,type="any",nomatch=0)
common$x = intersect(interval(common$on.effort, common$off.effort),
interval(common$i.on.effort, common$i.off.effort))
我想最终得到一个表,它是“obs”的子集,并且只包含“common$x”中的间隔所覆盖的行。我曾希望使用 foverlaps 找到相交间隔中的行,并为我的目击创建“点”间隔
obs[, sighting2 := sighting]
但是 foverlaps 想要每个间隔的“开始”和“结束”在单独的列中,这不是间隔存储在 common$x 中的方式。
我希望我的输出看起来像这样
sighting encounter what
2018-08-12 16:30:00 1 frog
2018-08-12 16:35:00 1 frog
2017-08-14 11:12:13 4 bird
我会很感激任何提示。也许我之前可以更有效率?谢谢。
解决方案
我认为即使您在平台之间有不同的观察数字,这也应该有效。如上所述使用您的obs
,platformA
和platformB
数据,或多或少地为两个平台设置间隔,就像您在上面所做的那样common
:
common = intersect(interval(platformA$on.effort, platformA$off.effort),
interval(platformB$on.effort, platformB$off.effort))
您应该能够使用%within%
来检查是否存在目击事件属于共同间隔的情况:
obs$both.seen <- sapply(obs$sighting, function(s){
any(s %within% common)
})
或者
obs[, both.seen := sapply(sighting, function(x) any(x %within% common))]
新obs
:
> obs
sighting encounter what both.seen
1: 2018-08-12 16:30:00 1 frog TRUE
2: 2018-08-12 16:35:00 1 frog TRUE
3: 2018-08-12 16:38:00 1 toad FALSE
4: 2107-08-13 15:13:00 2 bird FALSE
5: 2107-08-13 16:13:00 3 goat FALSE
6: 2017-08-14 11:12:13 4 bird TRUE
获得所需输出的子集:
obs <- obs[both.seen == 1][, both.seen := NULL][]
> obs
sighting encounter what
1: 2018-08-12 16:30:00 1 frog
2: 2018-08-12 16:35:00 1 frog
3: 2017-08-14 11:12:13 4 bird
推荐阅读
- c++ - 为表示一个或多个操作的 C++ 函数查找 int 参数的类型安全替换
- git - 在drupal 8中安装模块时作曲家抛出git错误
- typescript - 将类型分配给可能不需要解析的 JSON 类型
- java - 活动中的 Android 工作室名称选项卡
- sql - 我可以合并或加入这些表吗?
- c# - 如何在 ASP.NET 中使用视图模型?
- php - 即使 WP_DEBUG=true,Wordpress 也不会写入 debug.log
- quill - Quill:如何获取包含样式的 html 数据?
- python - 当您在 if 逻辑语句中调用修改函数时,是否会编辑 2D 列表?
- javascript - 如何使用 Javascript 本地存储从 HTML 文本框中存储值?