You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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_idabx_dateantibioticepisode
P0012023-01-01Amoxicillin1
P0012023-01-05Ciprofloxacin1
P0012023-01-10Azithromycin1
P0012023-01-18Amoxicillin2
P0012023-01-20Ciprofloxacin2
P0022023-02-02Azithromycin1
P0022023-02-08Amoxicillin1
P0022023-02-15Ciprofloxacin2
P0022023-02-22Azithromycin2
P0032023-03-01Amoxicillin1
P0032023-03-06Ciprofloxacin1
P0032023-03-14Azithromycin2

Key Notes

  • Date Format: Make sure your abx_date column is converted to a Date type with as.Date() first—otherwise the interval calculation will fail.
  • Customization: If you want to group episodes by antibiotic type too, add antibiotic to the group_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:06:02