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)
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 | 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 |
return_df | single logical, if |
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)
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.
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 1count_codes(mydf)#> [1] 2 1#> r s #> 2 1wide <- 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() }