count_codes takes a data frame with a column for visit_name and another for ICD-9 code, and returns the number of distinct codes for each patient.

count_codes(x, visit_name = get_visit_name(x), return_df = FALSE)

Arguments

x

data frame with one row per patient, and a true/false or 1/0 flag for each column. By default, the first column is the patient identifier and is not counted. If visit_name is not specified, the first column is used.

visit_name

The name of the column in the data frame which contains the patient or visit identifier. Typically this is the visit identifier, since patients come leave and enter hospital with different ICD-9 codes. It is a character vector of length one. If left empty, or NULL, then an attempt is made to guess which field has the ID for the patient encounter (not a patient ID, although this can of course be specified directly). The guesses proceed until a single match is made. Data frames may be wide with many matching fields, so to avoid false positives, anything but a single match is rejected. If there are no successful guesses, and visit_id was not specified, then the first column of the data frame is used.

return_df

single logical, if TRUE, return the result as a data frame with the first column being the visit_name, and the second being the count. If visit_name was a factor or named differently in the input, this is preserved.

Value

vector of the count of comorbidities for each patient. This is sometimes used as a metric of comorbidity load, instead of, or in addition to metrics like the Charlson Comorbidity Index (aka Charlson Score)

Details

The visit_name field is typically the first column. If there is no column called visit_name and visit_name is not specified, the first column is used.

Examples

mydf <- data.frame( visit_name = c("r", "r", "s"), icd9 = c("441", "412.93", "042") ) count_codes(mydf, return_df = TRUE)
#> visit_name icd_count #> 1 r 2 #> 2 s 1
count_codes(mydf)
#> [1] 2 1
cmb <- icd9_comorbid_quan_deyo(mydf, short_code = FALSE, return_df = TRUE) count_comorbid(cmb)
#> r s #> 2 1
wide <- data.frame( visit_name = c("r", "s", "t"), icd9_1 = c("0011", "441", "456"), icd9_2 = c(NA, "442", NA), icd9_3 = c(NA, NA, "510") ) count_codes_wide(wide)
#> r s t #> 1 2 2
# or: if (FALSE) { library(magrittr, warn.conflicts = FALSE) wide %>% wide_to_long() %>% count_codes() }