convert matrix of comorbidities into data frame, preserving visit_name information

comorbid_mat_to_df(
  x,
  visit_name = "visit_id",
  stringsAsFactors = getOption("stringsAsFactors")
)

Arguments

x

Matrix of comorbidities, with row and columns names defined

visit_name

Single character string with name for new column in output data frame. Everywhere else, visit_name describes the input data, but here it is for output data.

stringsAsFactors

Single logical value, describing whether the resulting data frame should have strings, e.g. visit_id converted to factor. Default is to follow the current session option. This is identical to the argument used in, among other base functions as.data.frame.

See also

Examples

longdf <- icd_long_data( visit_id = c("a", "b", "b", "c"), icd9 = as.icd9(c("441", "4240", "443", "441")) ) mat <- icd9_comorbid_elix(longdf) class(mat)
#> [1] "matrix" "array"
typeof(mat)
#> [1] "logical"
#> [1] "a" "b" "c"
df.out <- comorbid_mat_to_df(mat) stopifnot(is.data.frame(df.out)) # output data frame has a factor for the visit_name column stopifnot(identical(rownames(mat), as.character(df.out[["visit_id"]]))) df.out[, 1:4]
#> visit_id CHF Arrhythmia Valvular #> 1 a FALSE FALSE FALSE #> 2 b FALSE FALSE TRUE #> 3 c FALSE FALSE FALSE
# when creating a data frame like this, stringsAsFactors uses # the system-wide option you may have set e.g. with # options("stringsAsFactors" = FALSE). is.factor(df.out[["visit_id"]])
#> [1] FALSE