只需使用p[, comorbid_names] == 1
将为您提供所选疾病的TRUE/FALSE值表。要将患者姓名或ID添加到该列表中,请使用cbind
,如下所示:cbind(p["patient_id"], p[, comorbid_names] == 1)
其中“patient_id”是标识患者的列的名称。
下面是一个完整可重复的例子:
comorbid_names <- c("chd", "heart_failure","stroke", "hypertension",
"diabetes", "copd", "epilepsy", "hypothyroidism",
"cancer", "asthma", "ckd_stage3", "ckd_stage4",
"ckd_stage5", "atrial_fibrilation", "learning_disability",
"peripheral_arterial_disease", "osteoporosis")
all_morbidities <- c("chd", "heart_failure","stroke", "hypertension",
"diabetes", "copd", "epilepsy", "hypothyroidism",
"cancer", "asthma", "ckd_stage3", "ckd_stage4",
"ckd_stage5", "atrial_fibrilation", "learning_disability",
"peripheral_arterial_disease", "osteoporosis",
"hairyitis", "jellyitis", "transparency")
# Create dummy data frame "p" with patient ids and whether or not they suffer from each condition
patients <- data.frame(patient_id = 1:20)
conditions <- matrix(sample(0:1, nrow(patients)*length(all_morbidities), replace=TRUE),
nrow(patients),
length(all_morbidities))
p <- cbind(patients, conditions)
names(p) <- c(names(patients), all_morbidities)
# Final step: get patient IDs and whether they suffer from specific morbidities
comorbidities <- cbind(p["patient_id"], p[, comorbid_names] == 1)
如果你想只选择那些从发病的至少一个受苦的病人,这样做:
comorbidities[rowSums(comorbidities[-1]) != 0]
嗨@ Rospa-如果您得到的任何答案可以解决您的问题,您可以将其标记为已接受 – HAVB