2016-11-22 53 views
4

我有一个变量名称“comorbid_names”的列表。我想选择那些在“合并症”中存在合并症的人。但是,如果它们是真的,我想选择变量名称。错误:不支持使用矩阵或列进行列索引

例如患者1具有 “CHD” 而已,因此仅将被显示为TRUE

comorbid_names [1] "chd" "heart_failure" "stroke"
[4] "hypertension" "diabetes" "copd"
[7] "epilepsy" "hypothyroidism" "cancer"
[10] "asthma" "ckd_stage3" "ckd_stage4"
[13] "ckd_stage5" "atrial_fibrilation" "learning_disability"
[16] "peripheral_arterial_disease" "osteoporosis"
class(comorbid_names) [1] "character"

合并症< - 名称(P [,comorbid_names] [P [,comorbid_names] == 1])

在这一点上,我得到这个错误

错误:不支持使用矩阵或阵列的列索引

我不完全知道为什么,但我认为这是与comorbid_names做字符

有没有人有建议?

+0

嗨@ Rospa-如果您得到的任何答案可以解决您的问题,您可以将其标记为已接受 – HAVB

回答

0

只需使用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] 
6

如果ptibbledata.frame相反或除data.frame之外,您可能正在处理以下内容:

https://blog.rstudio.org/2016/03/24/tibble-1-0-0/

看帖子的底部:

Interacting with legacy code

A handful of functions are don’t work with tibbles because they expect df[, 1] to return a vector, not a data frame. If you encounter one of these functions, use as.data.frame() to turn a tibble back to a data frame:

class(as.data.frame(tbl_df(iris)))

你可能会做p <- as.data.frame(p)以及相处。

+0

HAVB,这实际上不是我接受答案的问题,那将是上面的Rospa。这是我对他们问题的回答。 –

+0

傻我。感谢您的支持,并为此造成的不便深表歉意! – HAVB