小组按日期差异
我想按日期差异进行分组。
例如,如果设施 A 有 7 个病例,但前 5 个病例发生在最后 2 个病例的 14 天之前,我希望它们位于两个不同的组中(参见下面的示例)
位置 | 地址 | start_date | start_date_diff | Group |
---|---|---|---|---|
Facility A | 123 main 2/7/2022 | 1 | 0 | 1 |
设施 A | 主街 | 2/11/2022 | 4 | 123 |
设施 A | 123 主街 | 2/11/2022 | 0 | 1 |
设施 A | main st | 2/11/2022 | 0 | 1 |
设施 A | 123 | 123 main st 2/12/2022 | 1 | 1 |
设施 A | 123 main st | 3/12/2022 | 28 | 2 |
设施 A | 123 main st | 3/ 17/2022 | 5 | 2 |
设施 B | 55福特 路 | 3/16/2022 | 0 | 3 |
设施 B | 55 福特 路 | 3/16/2022 | 0 | 3 |
设施 C | 1 阶梯大道 | 3/16/2022 | 0 | 4 |
设施 C | 1 阶梯大道 | 3/20/2022 | 4 | 4 |
设施 C | 1 阶梯大道 | 3/22/2022 | 2 | 4 |
这是我的代码,所以到目前为止:
我被困在如何通过个体观察之间的日期差异进一步对它们进行分组。
I want to group within a group by date difference.
For example, if there are 7 cases in facility A, but the first 5 cases happened before 14 days of the last 2 cases, I want them to be in two different groups (see below example)
location | address | start_date | start_date_diff | Group |
---|---|---|---|---|
Facility A | 123 main st | 2/7/2022 | 0 | 1 |
Facility A | 123 main st | 2/11/2022 | 4 | 1 |
Facility A | 123 main st | 2/11/2022 | 0 | 1 |
Facility A | 123 main st | 2/11/2022 | 0 | 1 |
Facility A | 123 main st | 2/12/2022 | 1 | 1 |
Facility A | 123 main st | 3/12/2022 | 28 | 2 |
Facility A | 123 main st | 3/17/2022 | 5 | 2 |
Facility B | 55 ford rd | 3/16/2022 | 0 | 3 |
Facility B | 55 ford rd | 3/16/2022 | 0 | 3 |
Facility C | 1 step ave | 3/16/2022 | 0 | 4 |
Facility C | 1 step ave | 3/20/2022 | 4 | 4 |
Facility C | 1 step ave | 3/22/2022 | 2 | 4 |
here is my code so far:
I am stuck on how to group them further by the date difference between individual observations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设我们还没有计算
diff
,并且我们需要将start_date
转换为算术上有用的东西。data.table
dplyr
数据
Assuming we don't already
diff
calculated, and that we need to convertstart_date
into something arithmetically useful.data.table
dplyr
Data