【发布时间】:2017-11-04 20:45:06
【问题描述】:
我在 R 中有两个数据框:A.df 和 B.df。第一个包含 N 行,每行是在特定日期和地点发生的事件。
第二个是在特定日期和地点接受采访的个人列表。
对于每个人,我想统计在面试日期之前的某个时间范围内,在个人面试地点的同一地点发生的事件的数量。
假设时间范围是面试日期前 x 天,我已经计算出该日期并存储在变量 xdaysbefore 中。
下面是数据框的样子
A.df
#Event Date Place
1 2015-05-01 1
2 2015-03-11 1
3 2015-07-04 2
4 2015-05-10 3
B.df
#Individual Date of Interview Place xdaysbefore
1 2016-07-11 1 2014-09-11
2 2016-05-07 3 2014-07-04
3 2016-08-09 2 2014-03-22
4 2016-01-10 3 2014-09-17
注意Date、Date of Interview和xdaysbefore都在Date R class中
我如何根据事件发生的地点和采访的个人地点为B.df 中的每个人计算在Date of Interview - xdaysbefore 时间范围内发生的事件。
我对@987654335@ 的期望如下所示:
B.df
#Individual Date of Interview Place xdaysbefore CountedEvents
1 2016-07-11 1 2014-09-11 2
2 2016-05-07 3 2014-07-04 1
3 2016-08-09 2 2014-03-22 1
4 2016-01-10 3 2014-09-17 1
其中CountedEvents 是在Date of Interview - xdaysbefore 时间范围内以及在我接受采访的同一地点发生的事件数。
【问题讨论】:
-
你能提供一个reproducible example(我们可以复制/粘贴到R中的东西)吗?
-
所以基本上,您想在
B.df中查找一个位置,在同一位置xdaysbefore to Date Of interview范围内A.df中发生的事件数。