【发布时间】:2018-07-07 14:50:49
【问题描述】:
我想按患者 ID 和日期将 df1(包含患者的治疗间隔)与 df2(包含实验室值)合并,以使实验室值的日期在药物开始日期后 5 天内.见下文
对于 df1:
ID = c(2, 2, 2, 2, 3, 5)
Medication = c("aspirin", "aspirin", "aspirin", "tylenol", "lipitor", "advil")
Start.Date = c("05/01/2017", "05/05/2017", "06/20/2017", "05/01/2017", "05/06/2017", "05/28/2017")
Stop.Date = c("05/04/2017", "05/10/2017", "06/27/2017", "05/15/2017", "05/12/2017", "06/13/2017")
df1 = data.frame(ID, Medication, Start.Date, Stop.Date)
ID Medication Start.Date Stop.Date
2 aspirin 05/01/2017 05/30/2017
2 tylenol 05/01/2017 05/15/2017
3 lipitor 05/06/2017 05/18/2017
5 advil 05/28/2017 06/13/2017
对于 df2:
ID = c(2,2,2,3,3,5)
Lab.date = c("04/30/2017", "05/03/2017", "05/15/2017", "05/05/2017", "05/18/17", "05/15/2017")
Lab.wbc = c(5.4, 3.2, 7.1, 6.0, 10.8, 11.3)
df2 = data.frame(ID, Lab.date, Lab.wbc)
ID Lab.date Lab.wbc
2 04/30/2017 5.4
2 05/03/2017 3.2
2 05/15/2017 7.1
3 05/05/2017 6.0
3 05/18/2017 10.8
5 05/15/2017 11.3
合并应导致以下情况,其中 Lab.date 距离药物开始日期 + 或 - 5 天:
ID Medication Start.Date Stop.Date Lab.date Lab.wbc
2 aspirin 05/01/2017 05/30/2017 04/30/2017 5.4
2 aspirin 05/01/2017 05/30/2017 05/03/2017 3.2
2 tylenol 05/01/2017 05/15/2017 04/30/2017 5.4
2 tylenol 05/01/2017 05/15/2017 05/03/2017 3.2
3 lipitor 05/06/2017 05/18/2017 05/05/2017 6.0
【问题讨论】:
标签: r dataframe merge date-range