使用R测试日期是否出现在多个日期范围内 [英] Test if date occurs in multiple date ranges with R
问题描述
我有一个具有多个日期范围的数据框(准确地说是45个):
I have a data frame with multiple date ranges (45 to be exact):
Range Start End
1 2014-01-01 2014-02-30
2 2015-01-10 2015-03-30
3 2016-04-20 2016-10-12
... ... ...
它们永远不会重叠
我还有一个具有各种事件日期(200K +)的数据框:
I also have a data frame with various event dates (200K+):
Event Date
1 2014-01-02
2 2014-03-20
3 2015-04-01
4 2016-08-18
... ...
我要测试这些日期是否在以下任意范围内:
I want to test if these dates fall within any of these ranges:
Event Date InRange
1 2014-01-02 TRUE
2 2014-03-20 FALSE
3 2015-04-01 FALSE
4 2016-08-18 TRUE
...
什么进行此测试的最佳方法是什么?我看过lubridate的 和 interval 函数以及各种Stackoverflow问题,但找不到一个好的解决方案。
What is the best way to perform this test? I have looked at lubridate's between and interval functions as well as various Stackoverflow questions, but cannot find a good solution.
推荐答案
您可以从第一个数据框创建一个日期范围的向量,然后使用%in%
运算符检查是否每个活动的日期在此日期范围内。假设您的第一个数据帧为 dateRange
,第二个数据为 events
,则将上述逻辑放在一行中:
You can create a vector of your date range from the first data frame, then use %in%
operator to check if each date of your events is in this date range. Assuming your first data frame is dateRange
, and second events
, putting the above logic in one line would be:
events$InRange <- events$Date %in% unlist(Map(`:`, dateRange$Start, dateRange$End))
events
Event Date InRange
1 1 2014-01-02 TRUE
2 2 2014-03-20 FALSE
3 3 2015-04-01 FALSE
4 4 2016-08-18 TRUE
在哪里使用了地图
创建日期范围向量。 地图
与组合:
运算符从开始$ c创建日期范围列表$ c>到
方便。 End
。接近列表的某处(2014-01-01:2014-02-30,2015-01-10:2015-03-30,2016-04-20:2016-10-12 ...)
(象征性地,无效),使用 unlist
,我们将其展平为日期范围的向量,然后可以将其与<$ c $一起使用c>%in%
Where we used the Map
to create the date range vector. Map
combined with :
operator create a list of date range from the Start
to the End
. Somewhere close to list(2014-01-01 : 2014-02-30, 2015-01-10 : 2015-03-30, 2016-04-20 : 2016-10-12 ...)
(symbolically, not valid), with the unlist
, we flatten it as a vector of date range which could then be used with %in%
conveniently.
这篇关于使用R测试日期是否出现在多个日期范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!