在R中为电子病历数据创建7天内抗生素用药的episode指示变量
Hey there! Let's work through creating that episode variable for your EHR antibiotic data. This is a super common task in clinical data analysis, so I'll walk you through it step by step with simulated data and clear code.
Step 1: Simulate a Realistic EHR Dataset
First, let's replicate a dataset similar to what you're working with—we'll include patient IDs, antibiotic prescription dates, and a sample antibiotic type for context:
library(tidyverse) # Set seed for reproducibility set.seed(123) ehr_data <- tibble( patient_id = rep(c("P001", "P002", "P003"), times = c(5, 4, 3)), abx_date = as.Date(c( "2023-01-01", "2023-01-05", "2023-01-10", "2023-01-18", "2023-01-20", "2023-02-02", "2023-02-08", "2023-02-15", "2023-02-22", "2023-03-01", "2023-03-06", "2023-03-14" )), antibiotic = sample(c("Amoxicillin", "Ciprofloxacin", "Azithromycin"), 12, replace = TRUE) ) # View raw data print(ehr_data)
Step 2: Create the episode Variable
The goal is to group records where the time between consecutive prescriptions is 7 days or less as the same episode. We'll use dplyr window functions to handle this per patient:
ehr_data_with_episode <- ehr_data %>% # Group by patient first—episodes are patient-specific! group_by(patient_id) %>% # Sort dates to ensure interval calculations are accurate arrange(abx_date, .by_group = TRUE) %>% # Calculate days between current and previous prescription mutate(days_since_last = as.numeric(abx_date - lag(abx_date, default = first(abx_date)))) %>% # Flag when a new episode starts (first record OR interval >7 days) mutate(new_episode = if_else(days_since_last > 7 | is.na(days_since_last), 1, 0)) %>% # Cumulative sum of flags gives unique episode numbers per patient mutate(episode = cumsum(new_episode)) %>% # Clean up intermediate columns (optional) select(-days_since_last, -new_episode) %>% ungroup() # View final result print(ehr_data_with_episode)
Expected Output
Here's what the final dataset will look like (matching your grouping requirement):
| patient_id | abx_date | antibiotic | episode |
|---|---|---|---|
| P001 | 2023-01-01 | Amoxicillin | 1 |
| P001 | 2023-01-05 | Ciprofloxacin | 1 |
| P001 | 2023-01-10 | Azithromycin | 1 |
| P001 | 2023-01-18 | Amoxicillin | 2 |
| P001 | 2023-01-20 | Ciprofloxacin | 2 |
| P002 | 2023-02-02 | Azithromycin | 1 |
| P002 | 2023-02-08 | Amoxicillin | 1 |
| P002 | 2023-02-15 | Ciprofloxacin | 2 |
| P002 | 2023-02-22 | Azithromycin | 2 |
| P003 | 2023-03-01 | Amoxicillin | 1 |
| P003 | 2023-03-06 | Ciprofloxacin | 1 |
| P003 | 2023-03-14 | Azithromycin | 2 |
Key Notes
- Date Format: Make sure your
abx_datecolumn is converted to aDatetype withas.Date()first—otherwise the interval calculation will fail. - Customization: If you want to group episodes by antibiotic type too, add
antibioticto thegroup_by()call. - Edge Cases: This logic groups based on consecutive record intervals. If a record is within 7 days of an earlier record but not the immediate previous one, it will still be part of the new episode (which aligns with standard clinical definitions of treatment episodes).
内容的提问来源于stack exchange,提问作者dlemas
相关产品推荐
相关产品推荐

