基于R语言统计特定时间窗内患者急诊就诊次数及新增统计列需求
Alright, let's break down your two tasks with practical R code—since you're working with a hospital visit dataset that has unique patient IDs (MRN) and unique visit IDs (hospital_account), I'll assume your data includes key columns like admission_date, discharge_date, and admission_type (with values like "Inpatient" and "ER"). Adjust column names as needed for your actual dataset!
First, let's make sure your date columns are formatted correctly (critical for time-based tasks later), then add columns to count observations tied to each patient.
Step 1: Load Required Libraries and Clean Dates
library(dplyr) library(lubridate) # Essential for date manipulation # Convert date columns to proper Date format (skip if already done) df <- df %>% mutate( admission_date = ymd(admission_date), discharge_date = ymd(discharge_date) )
Step 2: Add Columns for Specific Counts
Here are two common scenarios—pick the one that fits your "specific observation" need:
Scenario A: Total Visits Per Patient
Add a column showing how many total visits each patient has made (across all admission types):
df <- df %>% group_by(MRN) %>% mutate(total_patient_visits = n()) %>% # Counts all rows per MRN ungroup()
Scenario B: Total ER Visits Per Patient
Add a column showing how many times each patient has visited the ER (regardless of current visit type):
df <- df %>% group_by(MRN) %>% mutate(total_patient_ER_visits = sum(admission_type == "ER", na.rm = TRUE)) %>% ungroup()
For this task, we'll focus on Inpatient admissions and count how many ER visits the same patient had in the 30 days leading up to their inpatient admission (adjust the time window logic if you need post-admission counts instead).
Method 1: Efficient Join Approach (Best for Large Datasets)
This method is faster for big datasets by first isolating ER visits, then matching them to inpatient records:
# Step 1: Extract all ER visit records with patient ID and admission date ER_visit_data <- df %>% filter(admission_type == "ER") %>% select(MRN, ER_admission_date = admission_date) # Step 2: Match ER visits to inpatient admissions and count those within 30 days inpatient_with_ER_counts <- df %>% filter(admission_type == "Inpatient") %>% # Focus only on inpatient records left_join(ER_visit_data, by = "MRN") %>% # Link ER visits to the same patient # Check if ER visit was within 30 days before inpatient admission mutate(within_30_days = ER_admission_date >= (admission_date - days(30)) & ER_admission_date < admission_date) %>% # Count valid ER visits per inpatient admission group_by(MRN, hospital_account) %>% # Group by unique patient + unique visit mutate(ER_visits_30days_prior = sum(within_30_days, na.rm = TRUE)) %>% ungroup() %>% # Clean up temporary columns select(-ER_admission_date, -within_30_days)
Method 2: Intuitive Map Approach (Better for Small/Medium Datasets)
If you prefer a more readable (but slightly slower) approach, use purrr to iterate over each inpatient record and count matching ER visits:
library(purrr) df <- df %>% mutate( ER_visits_30days_prior = ifelse( admission_type == "Inpatient", # Only calculate for inpatient records # For each inpatient visit, count ER visits in the 30-day window map2_dbl( .x = MRN, .y = admission_date, ~ df %>% filter( MRN == .x, admission_type == "ER", admission_date >= .y - days(30), admission_date < .y ) %>% nrow() ), NA_real_ # Set to NA for non-inpatient records ) )
Notes:
- If you need to count ER visits after inpatient discharge instead, adjust the date condition to
ER_admission_date > discharge_date & ER_admission_date <= discharge_date + days(30). - Double-check that your date columns are properly formatted (use
class(df$admission_date)to confirm it'sDatetype).
内容的提问来源于stack exchange,提问作者Brad

