Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: unsupported use of matrix or array for column indexing

I have a list of variables name "comorbid_names". And I want to select people who have those comorbidities in "comorbidities". However, I want to select the variable names if they are true.

For example patient 1 has "chd" only, therefore only that will be displayed as 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"

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

At this point I get this error

Error: Unsupported use of matrix or array for column indexing

I am not entirely sure why, but I think it's to do with comorbid_names being character

Does anyone have an advice?

like image 770
Rospa Avatar asked Nov 22 '16 11:11

Rospa


1 Answers

If p is a tibble as opposed to or in addition to a data.frame, you might be dealing with the following:

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

Look at the bottom of the post:

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)))

You might get along by doing p <- as.data.frame(p) as well.

like image 152
Brian Albert Monroe Avatar answered Sep 27 '22 22:09

Brian Albert Monroe