Error: unsupported use of matrix or array to index columns

I have a list of variables name "comorbid_names". And I want to choose people who have concomitant concomitant "concomitant diseases." However, I want to select variable names if they are true.

For example, patient 1 has only "chd", so only this will display as TRUE

comorbid_names [1] "chd" "heart_failure" "stroke"
 [4] "hypertension" "diabetes" "cop"
 [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"

comorbidities <- names (p [, comorbid_names] [p [, comorbid_names] == 1])

At this moment I get this error

Error: unsupported using matrix or array to index columns

I'm not quite sure why, but I think it should be done with comorbid_names being a character

- ?

+4
2

p tibble, , :

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

:

tibbles, , df [, 1] , . , as.data.frame(), :

(as.data.frame(tbl_df ( )))

p <- as.data.frame(p).

+6

p[, comorbid_names] == 1, TRUE/FALSE . , 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]
0

Source: https://habr.com/ru/post/1661576/


All Articles