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

如何在R语言的DF_1数据框中新增72小时内再入院状态判定变量

How to Add a 72-Hour Readmission Flag to Your R Data Frame

Hey there! Let's break down how to create that PATIENT_READMISSION_72H variable you need. The key steps are converting your date-time strings to a usable format, grouping by patient ID to track their admission/discharge history, and then calculating the time between consecutive stays.

Step 1: Convert Date-Time Columns to POSIXct Format

First, we need to turn those character strings for admission and discharge times into actual date-time objects so we can calculate time differences. We'll use as.POSIXct() with the correct format (%d/%m/%Y %H:%M since your dates follow day/month/year):

# Convert character date-time values to POSIXct (usable date-time objects)
DF_1$PATIENT_ADMISSION <- as.POSIXct(DF_1$PATIENT_ADMISSION, format = "%d/%m/%Y %H:%M")
DF_1$PATIENT_DISCHARGE <- as.POSIXct(DF_1$PATIENT_DISCHARGE, format = "%d/%m/%Y %H:%M")

Step 2: Group by Patient ID and Calculate Readmission Time Intervals

Next, we'll use the dplyr package (a go-to tool for tidy data manipulation) to group records by patient ID, sort stays chronologically, and check the time between a patient's current admission and their previous discharge:

# Install dplyr if you haven't already
# install.packages("dplyr")
library(dplyr)

DF_1 <- DF_1 %>%
  # Group records by patient ID to analyze each patient's stays separately
  group_by(ID) %>%
  # Sort stays by admission time to ensure we're checking in chronological order
  arrange(PATIENT_ADMISSION) %>%
  # Calculate the time (in hours) between current admission and last discharge
  mutate(time_since_last_discharge = difftime(PATIENT_ADMISSION, lag(PATIENT_DISCHARGE), units = "hours")) %>%
  # Create the 72-hour readmission flag
  mutate(PATIENT_READMISSION_72H = case_when(
    # Mark 'Y' if 30-day readmission is true AND time since last discharge ≤72 hours
    PATIENT_READMISSION_30D == "Y" & !is.na(time_since_last_discharge) & time_since_last_discharge <= 72 ~ "Y",
    # Mark 'N' if 30-day readmission is true but time since last discharge >72 hours
    PATIENT_READMISSION_30D == "Y" & !is.na(time_since_last_discharge) & time_since_last_discharge > 72 ~ "N",
    # Leave empty for all other cases
    TRUE ~ ""
  )) %>%
  # Remove the helper column (optional, if you don't need to keep it)
  select(-time_since_last_discharge) %>%
  # Ungroup to return to a regular data frame
  ungroup()

Verify the Result

When you run this code, your DF_1 will match the desired output exactly:

  • Patient 222's second stay (marked as 30-day readmission) has a ~12.7-hour gap between discharge and readmission → flagged 'Y'
  • Patient 444's second stay has a ~120.8-hour gap → flagged 'N'
  • Patient 1010's third stay has a ~51.2-hour gap → flagged 'Y'

内容的提问来源于stack exchange,提问作者Bruno Avila

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 20:59:10