Elixhauser Comorbidities

# IMPORTANT SYNTAX NOTE:
#
# DO NOT USE the pipeOp `|>`
#
# While convenient, that is a R 4.1.0 feature at a minimum. Notable improvements
# to the pipeOp come in 4.2.0 and 4.2.1.  To keep this package dependent on R >=
# 3.5.0 do not use the pipeOp.

library(kableExtra)
options(qwraps2_markup = "markdown")
options(knitr.kable.NA = '')
knitr::opts_chunk$set(collapse = TRUE, fig.align = "center")
library(medicalcoder)
packageVersion("medicalcoder")

Introduction

The medicalcoder package implements several variants of the Elixhauser comorbidity algorithm.

IMPORTANT NOTE: Elixhauser 1998 and AHRQ Web used diagnosis-related group (DRG) codes as part of the methods. The medicalcoder package does not use DRG codes. This is consistent with the way these methods were implemented in @quan2005.

ICD Codes and Index Scores

End users may access three lookup tables relevant to Elixhauser variants.

str(get_elixhauser_codes())
str(get_elixhauser_index_scores())
str(get_elixhauser_poa())

Applying Elixhauser

The Elixhauser method considers if the diagnostic codes are for primary or secondary diagnoses. As such, the following call will throw a warning that the primarydx.var or primarydx argument was not set and that all conditions will be assumed to be secondary. To suppress this warning pass an appropriate value for either argument.

# will warn because primarydx and primarydx.var are both NULL
mdcr_results0 <-
  comorbidities(
    data = mdcr,
    id.vars = "patid",
    icdv.var = "icdv",
    icd.codes = "code",
    dx.var = "dx",
    flag.method = "current",
    poa = 1,
    method = "elixhauser_ahrq2025"
  )

# no warning
mdcr_results <-
  comorbidities(
    data = mdcr,
    id.vars = "patid",
    icdv.var = "icdv",
    icd.codes = "code",
    dx.var = "dx",
    flag.method = "current",
    poa = 1,
    method = "elixhauser_ahrq2025",
    primarydx = 0
  )

identical(mdcr_results, mdcr_results0)

The return object is a data.frame with 0/1 integer indicator columns for the relevant conditions, the id.vars (if applicable), num_cmrb, the number of comorbidities, cmrb_flag, a 0/1 indicator for presence of at least one comorbidity, and the mortality and readmission index scores.

str(mdcr_results)

Call summary() to get a list of summaries which can be used to generate summary tables.

summary(mdcr_results)
x <- summary(mdcr_results)$conditions
tab <-
  kableExtra::kbl(
    x = x,
    format = "html",
    caption = "Counts and percentages of patients in the mdcr example data sets with the Elixhauser @quan2005 comorbidities.",
    col.names = c("", "Count", "Percentage"),
    digits = 3
  )
tab <- kableExtra::kable_styling(tab, bootstrap_options = c("striped"), font_size = 10)
tab <- kableExtra::pack_rows(tab, group_label = "Comorbidity", start_row = 1, end_row = 38)
tab <- kableExtra::pack_rows(tab, group_label = "Total Comorbidities", start_row = 39, end_row = nrow(x))
tab

References



Try the medicalcoder package in your browser

Any scripts or data that you put into this service are public.

medicalcoder documentation built on Feb. 22, 2026, 5:08 p.m.