r - 如果它们在指定时间段内具有连续的时间间隔,则合并数据行
问题描述
我有一个包含 Start.Date 和 Stop.Date 的患者药物数据集。每个都以一行表示。我想合并代表相同药物的行,但前提是后面间隔的 Start.Date 在较早间隔的停止日期起 30 天内(或我选择指定的任意天数)。假设您有下面的数据框
ID = c(2, 2, 2, 2, 3, 5)
Medication = c("aspirin", "aspirin", "aspirin", "tylenol", "lipitor", "advil")
Start.Date = c("05/01/2017", "05/30/2017", "07/15/2017", "05/01/2017", "05/06/2017", "05/28/2017")
Stop.Date = c("05/04/2017", "06/10/2017", "07/27/2017", "05/15/2017", "05/12/2017", "06/13/2017")
df = data.frame(ID, Medication, Start.Date, Stop.Date)
ID Medication Start.Date Stop.Date
2 aspirin 05/01/2017 05/04/2017
2 aspirin 05/30/2017 06/10/2017
2 aspirin 07/15/2017 07/27/2017
2 tylenol 05/01/2017 05/15/2017
3 lipitor 05/06/2017 05/12/2017
5 advil 05/28/2017 06/13/2017
如果一个人的 Stop.Date 在下一个 Start.Date 之后的指定 30 天内,我想按 ID 和药物减少行。新的 Start.Date 和 Stop.Date 将包含两种药物的时间间隔以及它们之间的 30 天或更短的间隔。它应该如下所示:
ID Medication Start.Date Stop.Date
2 aspirin 05/01/2017 06/10/2017
2 aspirin 07/15/2017 07/27/2017
2 tylenol 05/01/2017 05/15/2017
3 lipitor 05/06/2017 05/12/2017
5 advil 05/28/2017 06/13/2017
解决方案
如果一个时期结束和下一个时期开始之间的间隔不超过 30 天,则 OP 已要求取消服药期。
下面的解决方案要求同一个人和同一药物的用药期永远不会重叠,这是一个合理的假设(并已检查)。
1.dplyr
library(dplyr)
library(magrittr)
min_gap <- 30
df %>%
# convert date strings to class Date
mutate_at(c("Start.Date", "Stop.Date"), lubridate::mdy) %>%
arrange(ID, Medication, Start.Date) %>%
group_by(ID, Medication) %T>%
# medication periods must not overlap for ID and Medication
{summarize(., tmp = all(Start.Date >= lag(Stop.Date, default = Start.Date[1] - 1))) %$%
stopifnot(all(tmp))} %>%
# count non-subsequent medication periods, i.e., with gaps of at least min_gap days
mutate(Medic.Period = cumsum(Start.Date > lag(Stop.Date, default = Start.Date[1]) + min_gap)) %>%
# determine start and stop dates for each collapsed period
group_by(ID, Medication, Medic.Period) %>%
summarise(Start.Date = first(Start.Date), Stop.Date = last(Stop.Date))
# A tibble: 5 x 5 # Groups: ID, Medication [?] ID Medication Medic.Period Start.Date Stop.Date <dbl> <fct> <int> <date> <date> 1 2 aspirin 0 2017-05-01 2017-06-10 2 2 aspirin 1 2017-07-15 2017-07-27 3 2 tylenol 0 2017-05-01 2017-05-15 4 3 lipitor 0 2017-05-06 2017-05-12 5 5 advil 0 2017-05-28 2017-06-13
该cumsum()
功能用于在遇到新的经期时增加服药期计数器,即在前一期停止和实际经期开始之间的间隔超过 30 天。
2.data.table
library(data.table)
min_gap <- 30
# coerce date strings to class Date
cols <- stringr::str_subset(names(df), "Date$")
setDT(df)[, (cols) := lapply(.SD, lubridate::mdy), .SDcols = cols][
# create medication counters for each ID and Medication
order(Start.Date),
Medic.Period := {
tmp <- shift(Stop.Date, fill = Start.Date[1] - 1)
stopifnot(all(Start.Date > tmp))
cumsum(Start.Date > tmp + min_gap)
},
by = .(ID, Medication)][
# aggregate to find the overal start and stop dates for each medication period
, .(Start.Date = min(Start.Date), Stop.Date = max(Stop.Date)),
by = .(ID, Medication, Medic.Period)]
ID Medication Medic.Period Start.Date Stop.Date 1: 2 aspirin 0 2017-05-01 2017-06-10 2: 2 aspirin 1 2017-07-15 2017-07-27 3: 2 tylenol 0 2017-05-01 2017-05-15 4: 3 lipitor 0 2017-05-06 2017-05-12 5: 5 advil 0 2017-05-28 2017-06-13
推荐阅读
- java - 嵌套异常是 java.lang.NoClassDefFoundError: org.springframework.boot.env.PropertySourcesLoader
- flutter - 居中并调整 FAB 大小
- svn - 如何在不更改修订号的情况下加载部分存储库转储
- sql - 在 SQL 开发人员中将日期时间格式转换为 sysdate 格式
- azure - 在 Azure Logic App 的结果视图中获取“无法获取”
- c - 如何在 C 编程中将数据保存在 .txt 文件中
- django - Django manage.py 文件的声纳云安全热点
- python - 从python中的文本中删除空字符串
- c - 这个内存泄漏在哪里?
- acumatica - 如何在 Acumatica 报告变量中连接刺痛