Ros*_*spa 5 arrays indexing matrix
我有一个变量列表名称"comorbid_names".我想选择那些在"合并症"中有合并症的人.但是,如果它们为true,我想选择变量名称.
例如,患者1仅具有"chd",因此仅将其显示为TRUE
comorbid_names [1]"chd""heart_failure""中风"
[4]"高血压""糖尿病""copd"
[7]"癫痫""甲状腺功能减退症""癌症"
[10]"哮喘""ckd_stage3""ckd_stage4"
[ 13]"ckd_stage5""atrial_fibrilation""learning_disability"
[16]"peripheral_arterial_disease""osteoporosis"
类(comorbid_names)[1]"character"
comorbidities < - names(p [,comorbid_names] [p [,comorbid_names] == 1])
此时我收到此错误
错误:不支持使用矩阵或数组进行列索引
我不完全确定为什么,但我认为这与comorbid_names是字符有关
有没有人有建议?
Bri*_*roe 16
如果p是a tibble或与a相反data.frame,您可能正在处理以下内容:
https://blog.rstudio.org/2016/03/24/tibble-1-0-0/
看看帖子的底部:
与遗留代码交互
少数函数不能与tibbles一起使用,因为它们期望df [,1]返回一个向量,而不是数据帧.如果遇到其中一个函数,请使用as.data.frame()将tibble转回数据框:
类(as.data.frame(tbl_df(光圈)))
你也可以相处p <- as.data.frame(p)得好.
只需使用p[, comorbid_names] == 1will 即可为您提供所选发病率的 TRUE/FALSE 值表。要将患者姓名或 ID 添加到该列表,请使用cbind,如下所示:cbind(p["patient_id"], p[, comorbid_names] == 1)其中“patent_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)
Run Code Online (Sandbox Code Playgroud)
如果您只想选择患有至少一种疾病的患者,请执行以下操作:
comorbidities[rowSums(comorbidities[-1]) != 0]
Run Code Online (Sandbox Code Playgroud)