Package {medicalcoder}


Title: A Unified and Longitudinally Aware Framework for ICD-Based Comorbidity Assessment
Version: 0.9.0
Description: Provides comorbidity classification algorithms such as the Pediatric Complex Chronic Conditions (PCCC), Charlson, and Elixhauser indices, supports longitudinal comorbidity flagging across encounters, and includes utilities for working with medical coding schemas such as the International Classification of Diseases (ICD).
Depends: R (≥ 3.5.0)
License: BSD_3_clause + file LICENSE
Language: en-US
Encoding: UTF-8
URL: http://www.peteredewitt.com/medicalcoder/, https://github.com/dewittpe/medicalcoder/
BugReports: https://github.com/dewittpe/medicalcoder/issues
LazyData: true
Suggests: data.table, dplyr, kableExtra, knitr, R.utils, rmarkdown, tibble, tidyr
VignetteBuilder: knitr, rmarkdown
Config/Needs/website: pkgdown, comorbidity, icdcomorbid, multimorbidity, dplyr, tidyr, tibble, odbc, DBI, RSQLite, pccc
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-07-11 23:11:40 UTC; dewittp
Author: Peter DeWitt ORCID iD [aut, cre, cov], Tell Bennett ORCID iD [ctb], Seth Russell ORCID iD [ctb], Meg Rebull ORCID iD [ctb], Vincent Rubinetti ORCID iD [cov]
Maintainer: Peter DeWitt <peter.dewitt@cuanschutz.edu>
Repository: CRAN
Date/Publication: 2026-07-11 23:40:02 UTC

medicalcoder

Description

An R package for working with ICD codes and comorbidity assessments.

Details

medicalcoder is a lightweight, base-R package for working with ICD-9 and ICD-10 diagnosis and procedure codes. It provides fast, dependency-free tools to look up, validate, and manipulate ICD codes, while also implementing widely used comorbidity algorithms such as Charlson, Elixhauser, and the Pediatric Complex Chronic Conditions (PCCC). Designed for portability and reproducibility, the package avoids external dependencies, requiring only ⁠R >= 3.5.0⁠, yet offers a rich set of curated ICD code libraries from the United States' Centers for Medicare and Medicaid Services (CMS), Centers for Disease Control (CDC), the World Health Organization (WHO), the Independent Health and Aged Care Pricing Authority (IHACPA), and from Sweden's National Board of Health and Welfare (Socialstyrelsen).

The package balances performance with elegance: its internal caching, efficient joins, and compact data structures make it practical for large-scale health data analyses, while its clean design makes it easy to extend or audit. Whether you need to flag comorbidities, explore ICD hierarchies, or standardize clinical coding workflows, medicalcoder provides a robust, transparent foundation for research and applied work in biomedical informatics.

Implementation

The medicalcoder package was intentionally designed and built to have zero dependencies beyond R version 3.5.0 (needed due to a change in data serialization) and zero imports. The package is completely self-contained for the purposes of installation and use.

This design choice was made for several reasons.

  1. Ease of installation:

    • The only requirement is ⁠R >= 3.5.0⁠.

    • No need for external files, downloads, or other packages for the ICD database.

  2. Works well with different data paradigms:

One of the reasons for focusing on building a self-contained package with no need for additional namespaces is to make installation and use in a restricted and secure systems easier. The author of this package routinely works on machines with extremely limited access to the world-wide web. As such, relying on any system dependencies or other R packages can become difficult because the machine may or may not have the needed software. So long as R >= 3.5.0 is available, medicalcoder will work.

A great deal of thought went into performance of the methods and the size of the package. The internal datasets, for example, are not stored in a structure that is easy to use for end users. When the package namespace is loaded, the needed internal lookup tables are generated and cached.

Author(s)

Maintainer: Peter DeWitt peter.dewitt@cuanschutz.edu (ORCID) [cover designer]

Authors:

Other contributors:

See Also

comorbidities(), get_icd_codes(), is_icd(),


Comorbidities

Description

Apply established comorbidity algorithms to ICD-coded data. Supported methods include several variants of the Charlson comorbidity system, Elixhauser, and the Pediatric Complex Chronic Conditions (PCCC).

Usage

comorbidities(
  data,
  icd.codes,
  method,
  id.vars = NULL,
  icdv.var = NULL,
  icdv = NULL,
  dx.var = NULL,
  dx = NULL,
  poa.var = NULL,
  poa = NULL,
  age.var = NULL,
  primarydx.var = NULL,
  primarydx = NULL,
  flag.method = c("current", "cumulative"),
  full.codes = TRUE,
  compact.codes = TRUE,
  subconditions = FALSE,
  mapping = c("precomputed", "regex")
)

Arguments

data

A data.frame in a "long" format. The input data.frame is expected to have one column of ICD codes (one code per row) with additional (optional) columns for patient/encounter ids, ICD version, diagnostic/procedure status, present-on-admission flags, primary diagnostic flags, or age.

icd.codes

Character scalar naming the column in data that contains ICD codes (character strings). Codes may be provided in full form (with decimal points, e.g., C84.2), compact form (dots omitted, e.g., C842), or any mix of the two. Matching against lookup tables is governed by icdv.var/icdv, dx.var/dx, and the full.codes / compact.codes flags.

method

Character string indicating the comorbidity algorithm to apply to data.

id.vars

Optional character vector of column names. When missing, the entire input data is treated as a single encounter from a single patient. If you want to set flag.method = "cumulative" then length(id.vars) >= 2 is expected. The last element should be the encounter order. It must not contain missing values, must not be a factor, and must be numeric, character, Date, or POSIXt. Character encounter order columns are allowed with a warning because they are sorted lexicographically.

icdv.var

Character scalar naming the column in data that indicates the ICD version. If present it must be numeric. Rows with values other than 9L or 10L will not be used to map ICD codes to comorbidities. icdv.var takes precedence over icdv if both are provided.

icdv

An integer value of 9L or 10L indicating that all data[[icd.codes]] are ICD version 9 or 10, respectively. Ignored (with a warning) if icdv.var is provided.

dx.var

Character scalar naming the column in data that indicates diagnostic (1L) vs procedural (0L) codes. If present it must be numeric. Rows with values other than 0L or 1L will not be used to map ICD codes to comorbidities. dx.var takes precedence over dx if both are provided.

dx

An integer indicating that all data[[icd.codes]] are diagnostic (1L) or procedure (0L) codes. Ignored (with a warning) if dx.var is provided.

poa.var

Character scalar naming the column with present-on-admission flags: integer 1L (present), 0L (not present), or NA. For AHRQ SAS-compatible present-on-admission indicators, convert "Y" and "W" to 1L, convert "N" and "U" to 0L, and convert blank or missing values to NA_integer_ before calling comorbidities(). PCCC and Charlson will only flag conditions when the code is present-on-admission. Elixhauser has a mix of conditions; some require present-on-admission while others do not. poa.var takes precedence over poa if both are provided.

poa

Integer scalar 0L or 1L. Use when all icd.codes share the same present-on-admission status. Ignored with a warning if poa and poa.var are both provided.

age.var

Character scalar naming the column in data that contains patient age in years. Only applicable to Charlson comorbidities.

primarydx.var

Character scalar naming the column in data that indicates whether data[[icd.codes]] are primary diagnostic codes (1L) or not (0L). Primary diagnosis is used only for Elixhauser and Charlson comorbidities and is ignored when the method is a PCCC variant. primarydx.var takes precedence over primarydx if both are provided.

primarydx

An integer value of 0L or 1L. If 0L, treat all codes as non-primary diagnoses; if 1L, treat all codes as primary diagnoses. Ignored, with a warning, if primarydx.var is provided.

flag.method

When flag.method = "current" (default) only codes associated with the current id.vars are considered when flagging comorbidities. The flag.method = "cumulative" option also considers prior encounters when flagging comorbidities. See Details.

full.codes, compact.codes

Logical; when TRUE compare data[[icd.codes]] against full and/or compact ICD codes in the method’s lookup tables. Full ICD codes include a decimal point (when applicable) and compact codes omit the decimal point. For example: B95.0 is the full ICD-10-CM diagnostic code for “Streptococcus, group A, as the cause of disease classified elsewhere,” whereas B950 is the associated compact code.

subconditions

Logical scalar; when TRUE, report both conditions and subconditions (PCCC only).

mapping

Character string specifying how data[[icd.codes]] should be mapped to comorbidity conditions. mapping = "precomputed" uses the precomputed ICD code-condition links included with medicalcoder and is the default. mapping = "regex" applies the method's regular expressions directly to the input ICD codes. See Details.

Details

When flag.method = "current", only codes from the index encounter contribute to flags. The flag.method = "cumulative" option lets prior encounters for the same id.vars combination contribute to condition flags. For the cumulative method to work, id.vars needs to be a character vector of length 2 or more. The last element is treated as the encounter identifier and must be sortable. For example, say you have data with a hospital, patient, and encounter id. The id.vars could be one of two entries: c("hospital", "patient", "encounter") or c("patient", "hospital", "encounter"). In both cases the return will be the same because the encounter identifier is unchanged regardless of whether hospital or patient is listed first.

It is critically important that the data[[tail(id.vars, 1)]] variable can be sorted. Just because your data is sorted in temporal order does not mean that the results will be correct if the tail(id.vars, 1) is not in the same order as the data. For example, say you had the following:

patid enc_id date
P1 10823090 Aug 2023
P1 10725138 Jul 2025

id.vars = c("patid", "enc_id") will give the wrong result as enc_id 10725138 would be sorted to come before enc_id 10823090. id.vars = c("patid", "date") would be sufficient input, assuming that date has been correctly stored. Adding a column enc_seq, e.g.,

patid enc_id date enc_seq
P1 10823090 Aug 2023 1
P1 10725138 Jul 2025 2

and calling comorbidities() with id.vars = c("patid", "enc_seq") will have better performance than using the date and will clear up any possible issues with non-sequential encounter ids from the source data.

For flag.method = "cumulative", the encounter order column must not contain missing values, must not be a factor, and must be numeric, character, Date, or POSIXt. Character encounter order columns are allowed, but they are sorted lexicographically; use an integer sequence, Date, or POSIXt column when possible.

Cumulative flagging + POA defaults:

When flag.method = "cumulative" and neither poa nor poa.var is supplied, the first encounter for a condition is treated as poa = 0L. Subsequent encounters for that condition are flagged as poa = 1L.

When flag.method = "current" and neither poa nor poa.var is supplied, then all codes will be considered present-on-admission. If poa was assumed to be 0, then in this case the only conditions that could be flagged are the Elixhauser conditions which are poa-exempt.

The mapping option controls how ICD codes are mapped to conditions. The default, mapping = "precomputed", uses a precomputed table that links valid ICD codes to comorbidity conditions. Those links are built from the ICD code sources included with medicalcoder: the United States Centers for Disease Control and Prevention (CDC), the Centers for Medicare and Medicaid Services (CMS), the World Health Organization (WHO), the Independent Health and Aged Care Pricing Authority (IHACPA) ICD-10 Australian Modification (ICD-10-AM) data, and Socialstyrelsen ICD-10-SE data.

mapping = "precomputed" is generally fastest and is the behavior used by medicalcoder before the mapping argument was added. mapping = "regex" applies the method's regular expressions directly to the input ICD codes. Regex mapping is currently implemented for Charlson methods and is useful when codes come from an ICD modification that may not be completely covered by the precomputed code-condition links, or when auditing a method.

Value

The return object will be slightly different depending on the value of method and subconditions.

References

See Also

Examples

pccc_v3.1_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "pccc_v3.1",
                flag.method = 'current',
                poa = 1L)
summary(pccc_v3.1_results)

pccc_v3.1_subcondition_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "pccc_v3.1",
                flag.method = 'current',
                poa = 1L,
                subconditions = TRUE)
summary(pccc_v3.1_subcondition_results)

charlson_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "charlson_quan2011",
                flag.method = 'current',
                poa = 1L)
summary(charlson_results)

elixhauser_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "elixhauser_ahrq2025",
                primarydx = 1L,
                flag.method = 'current',
                poa = 1L)
summary(elixhauser_results)

Get Charlson Codes

Description

Retrieve a copy of internal lookup tables for the ICD codes used in assessing Charlson comorbidities. The lookup includes indicator columns for each implemented Charlson variant.

Usage

get_charlson_codes()

Value

A data.frame with the following columns:

See Also

Examples

head(get_charlson_codes())
str(get_charlson_codes())


Get Charlson Index Scores

Description

Retrieve a copy of internal lookup tables of index scores used in assessing Charlson comorbidities. The lookup includes score columns for each implemented Charlson variant.

Usage

get_charlson_index_scores()

Value

A data.frame with the following columns:

See Also

Examples

head(get_charlson_index_scores())
str(get_charlson_index_scores())


Get Elixhauser Codes

Description

Retrieve a copy of internal lookup tables for the ICD codes used in assessing Elixhauser comorbidities.

Usage

get_elixhauser_codes()

Value

A data.frame with the following columns:

See Also

Examples

head(get_elixhauser_codes())
str(get_elixhauser_codes())


Get Elixhauser Index Scores

Description

Functions to get a copy of internal lookup tables for the ICD codes and index scores used in assessing Elixhauser comorbidities.

Usage

get_elixhauser_index_scores()

Value

A data.frame with the following columns:

See Also

Examples

head(get_elixhauser_index_scores())
str(get_elixhauser_index_scores())


Get Elixhauser Present-on-Admission Requirements

Description

Retrieve a copy of internal lookup table with details on which Elixhauser comorbidities do and do not require the associated ICD codes to be present-on-admission to be flagged.

Usage

get_elixhauser_poa()

Value

A data.frame with the following columns:

See Also

Examples

head(get_elixhauser_poa())
str(get_elixhauser_poa())


Get ICD Codes

Description

Retrieve a copy of the internal lookup table for all known ICD codes.

Usage

get_icd_codes(with.descriptions = FALSE, with.hierarchy = FALSE)

Arguments

with.descriptions

Logical scalar, if TRUE include the description of the codes.

with.hierarchy

Logical scalar, if TRUE include the ICD hierarchy.

Details

Sources

There are five sources of ICD codes.

Fiscal, Financial, and Calendar Years

When reporting years there is a mix of fiscal, financial, and calendar years.

Fiscal years are the United States Federal Government fiscal years, running from October 1 to September 30. For example, fiscal year 2013 started October 1, 2012 and ended on September 30, 2013.

Financial years are the Australian financial years used for IHACPA data, running from July 1 to June 30.

Calendar years run January 1 to December 31.

Within the ICD data there are columns known_start, known_end, assignable_start, assignable_end, desc_start and desc_end. For ICD codes with src == "cms", these are fiscal years. For codes with src == "ihacpa", these are financial years. For codes with src == "cdc", src == "socialstyrelsen", or src == "who" these are calendar years.

known_start is the first fiscal, financial, or calendar year (depending on source) that the medicalcoder package has definitive source data for. ICD-9-CM started in the United States in fiscal year 1980. The CDC extracts included in medicalcoder span fiscal years 1997–2012; the CMS ICD-9-CM/PCS extracts start in fiscal year 2006 and run through fiscal year 2015. As such 1997 is the earliest "known start" for ICD-9 within medicalcoder.

known_end is the last fiscal, financial, or calendar year (depending on source) for which we have definitive source data. For ICD-9-CM and ICD-9-PCS, CMS provides data through fiscal year 2015, while the CDC extracts stop at fiscal year 2012. For ICD-10-CM and ICD-10-PCS, which are active, it is just the last year of known data. ICD-10 from the WHO ends in 2019.

Header and Assignable Codes

"Assignable" indicates that the code is the most granular for the source. Ideally codes are reported with the greatest level of detail but that is not always the case. Also, the greatest level of detail can differ between sources. Example: C86 is a header code for cms and who because codes C86.0, C86.1, C86.2, C86.3, C86.4, C86.5, and C86.6 all exist in both standards. No code with a fifth digit exists in the who so all these four digit codes are 'assignable.' In the cms standard, C86.0 was assignable through fiscal year 2024. In fiscal year 2025 codes C86.00 and C86.01 were added making C86.0 a header code and C86.00 and C86.01 assignable codes.

Value

a data.frame

The default return has the following columns:

When with.descriptions = TRUE there are the following additional columns:

When with.hierarchy = TRUE there are the following additional columns:

See Also

is_icd(), lookup_icd_codes(), vignette(topic = "icd", package = "medicalcoder")

Examples

icd_codes <- get_icd_codes()
str(icd_codes)

# Explore the change in the assignable year for C86 code between CMS and
# WHO
subset(get_icd_codes(), grepl("^C86$", full_code))
subset(get_icd_codes(), grepl("^C86\\.\\d$", full_code))
subset(get_icd_codes(), grepl("^C86\\.0(\\d|$)", full_code))

is_icd("C86", headerok = FALSE) # FALSE
is_icd("C86", headerok = TRUE)  # TRUE
is_icd("C86", headerok = TRUE, src = "cdc") # Not a CDC mortality code

lookup_icd_codes("^C86\\.0\\d*", regex = TRUE)

Pediatric Complex Chronic Conditions ICD Codes

Description

Retrieve a copy of internal lookup tables for the ICD codes mapping to the Pediatric Complex Chronic Conditions (PCCC) conditions and subconditions by variant.

Usage

get_pccc_codes()

Value

a data.frame with the following columns

See Also

Examples

head(get_pccc_codes())
str(get_pccc_codes())

Pediatric Complex Chronic Condition and Subconditions

Description

Retrieve a copy of internal lookup tables for the syntactically valid and human readable labels of the Pediatric Complex Chronic Conditions (PCCC) conditions and subconditions.

Usage

get_pccc_conditions()

Value

A data.frame with the following character columns:

See Also

Examples

get_pccc_conditions()


Convert ICD Compact Codes to Full Codes

Description

Take an assumed ICD compact code string and convert to a full code based on the ICD version (9L or 10L) and type (diagnostic or procedure). This method only formats strings and does not validate the code(s).

Usage

icd_compact_to_full(x, icdv, dx)

Arguments

x

Character vector

icdv

Integer vector of allowed ICD versions. Use 9L and/or 10L. Defaults to both.

dx

Integer vector indicating allowed code type(s): 1L for diagnostic (ICD-9-CM, ICD-10-CM, ICD-10-AM, ICD-10-SE, CDC mortality, WHO), 0L for procedural (ICD-9-PCS, ICD-10-PCS). Defaults to both.

Value

A character vector the same length as x.

See Also

Other ICD tools: is_icd(), lookup_icd_codes()


Is ICD

Description

Answer the question "is the character string x a valid ICD code?" ICD codes should be character vectors. is_icd will assess for both "full codes" (decimal point present when appropriate) and "compact codes" (decimal point omitted).

ICD-10 code "C00" is a header code because the four-character codes C00.0, C00.1, C00.2, C00.3, C00.4, C00.5, C00.6, C00.7, C00.8, and C00.9 exist. Those four-character codes are assignable (as of 2025) because no five-character descendants (e.g., C00.40) exist.

When the source is the World Health Organization (WHO), CDC Mortality, or Socialstyrelsen, years refer to calendar years. CMS sources use the U.S. federal fiscal year, which starts on October 1 (e.g., fiscal year 2024 runs 2023-10-01 to 2024-09-30). IHACPA sources use the Australian financial year, which starts on July 1 and ends on June 30.

Usage

is_icd(
  x,
  icdv = c(9L, 10L),
  dx = c(1L, 0L),
  src = c("cdc", "cms", "ihacpa", "socialstyrelsen", "who"),
  year,
  headerok = FALSE,
  ever.assignable = missing(year),
  warn.ambiguous = TRUE,
  full.codes = TRUE,
  compact.codes = TRUE
)

Arguments

x

Character vector of ICD codes (full or compact form).

icdv

Integer vector of allowed ICD versions. Use 9L and/or 10L. Defaults to both.

dx

Integer vector indicating allowed code type(s): 1L for diagnostic (ICD-9-CM, ICD-10-CM, ICD-10-AM, ICD-10-SE, CDC mortality, WHO), 0L for procedural (ICD-9-PCS, ICD-10-PCS). Defaults to both.

src

Character vector of code sources. One or more of "cdc", "cms", "ihacpa", "socialstyrelsen", and "who". Defaults to all. CDC and CMS are from the United States (ICD-9/10-CM and ICD-9/10-PCS), IHACPA from Australia (ICD-10-AM), Socialstyrelsen from Sweden (ICD-10-SE), and the World Health Organization (ICD-10).

year

Numeric scalar. Calendar, fiscal, or financial year to reference. Default is the most current year available per source. For ICD-9, CMS data run through fiscal year 2015 and CDC extracts through 2012; ICD-10 sources are updated annually. Calendar year for WHO, CDC mortality, and Socialstyrelsen. U.S. federal fiscal year for CMS. Australian financial year for IHACPA.

headerok

Logical scalar. If FALSE (default), only assignable codes are considered valid; if TRUE, header codes are also accepted.

ever.assignable

Logical scalar. If TRUE then ignore year and return TRUE if the x was ever an assignable code.

warn.ambiguous

Logical scalar. If TRUE (default), warn when a code matches more than one ICD version and/or type (e.g., both CM and PCS).

full.codes

Logical scalar. If TRUE (default), match codes that include the decimal point where applicable.

compact.codes

Logical scalar. If TRUE (default), match codes without the decimal point.

Details

Similarly for ICD-9-CM: "055" is a header for measles; 055.0, 055.1, 055.2, 055.8, and 055.9 are assignable. Codes 055.3–055.6 do not exist. Code 055.7 is a header because 055.71 and 055.72 exist.

Some codes change status across years. For example, ICD-9-CM 516.3 was assignable in fiscal years 1997–2011 for the CDC extracts (2006–2011 for CMS) and became a header in 2012–2015.

Value

A logical vector the same length as x.

See Also

Other ICD tools: icd_compact_to_full(), lookup_icd_codes()

Examples

################################################################################
# Some ICD-9 diagnostic codes
x <- c("136.2", "718.60", "642.02")

is_icd(x, icdv =  9L, dx = 1L)
is_icd(x, icdv =  9L, dx = 0L)
is_icd(x, icdv = 10L, dx = 1L)
is_icd(x, icdv = 10L, dx = 0L)

is_icd(x, icdv = 9L, dx = 1L, headerok = TRUE)
is_icd(x, icdv = 9L, dx = 1L, year = 2006)

################################################################################
# ICD code with, or without a dot.  The ICD-9 diagnostic code 799.3 and ICD-9
# procedure code 79.93 both become 7993 when assessed against the ICD code look
# up tables.  As such "7993" is a valid ICD-9 diagnostic and procedure code,
# whereas 799.3 is only a valid dx code, and 79.93 is only a valid pr code.
# Further, codes such as ".7993", "7.993", "7993.", are all non-valid codes.

x <- c("7993", ".7993", "7.993", "79.93", "799.3", "7993.")
data.frame(
  x,
  dx = is_icd(x, icdv = 9L, dx = 1L),
  pr = is_icd(x, icdv = 9L, dx = 0L)
)

################################################################################
# example of a ICD-9 code that was assignable, but became a header when
# more descriptive codes were introduced: ICD-9 diagnostic code 516.3
lookup_icd_codes(paste0("516.3", c("", as.character(0:9))))

# ICD-9 code 516.3 was an assignable code through fiscal year 2011.
is_icd("516.3")

# If `year` is omitted, and `ever.assignable = FALSE` then the `year` is
# implied to be the max `known_end` year for ICD codes matched by `icdv`,
# `dx`, and `src`.
is_icd("516.3", ever.assignable = FALSE)

# when `year` is provided then `ever.assignable` is `FALSE` by default and
# the return is TRUE when 516.3 was assignable and FALSE otherwise.
is_icd("516.3", year = 2015)
is_icd("516.3", year = 2011)

# when year is a non-assignable year, but `ever.assignable = TRUE` the return
# will be TRUE.  Useful if you know the data is retrospective and collected
# through fiscal year 2015.
is_icd("516.3", year = 2015, ever.assignable = TRUE)

################################################################################
# Consider the string E010
#   - This could be an ICD-9-CM full code
#   - Could be an ICD-10-CM compact code
lookup_icd_codes("E010")
subset(get_icd_codes(with.descriptions = TRUE), grepl("^E010$", code))

is_icd("E010")
is_icd("E010", icdv = 9L) # FALSE because it is a header code and was never assignable
is_icd("E010", icdv = 9L, ever.assignable = TRUE) # FALSE
is_icd("E010", icdv = 9L, headerok = TRUE) # TRUE

Lookup ICD Codes

Description

Functions for working with ICD codes.

ICD-10 code "C00" is a header code because the four-character codes C00.0, C00.1, C00.2, C00.3, C00.4, C00.5, C00.6, C00.7, C00.8, and C00.9 exist. Those four-character codes are assignable (as of 2025) because no five-character descendants (e.g., C00.40) exist.

When the source is the World Health Organization (WHO), CDC Mortality, or Socialstyrelsen, years refer to calendar years. CMS sources use the U.S. federal fiscal year, which starts on October 1 (e.g., fiscal year 2024 runs 2023-10-01 to 2024-09-30). IHACPA sources use the Australian financial year, which starts on July 1 and ends on June 30.

Usage

lookup_icd_codes(
  x,
  regex = FALSE,
  full.codes = TRUE,
  compact.codes = TRUE,
  ...
)

Arguments

x

Character vector of ICD codes (full or compact form). When regex = TRUE, x must contain at least one non-empty, non-missing string.

regex

Logical scalar. If TRUE, treat x as regular expressions; if FALSE, use exact matching.

full.codes

Logical scalar. If TRUE (default), match codes that include the decimal point where applicable.

compact.codes

Logical scalar. If TRUE (default), match codes without the decimal point.

...

Passed to base::grep() when regex = TRUE

Details

ICD codes should be character vectors. These tools work with either "full codes" (decimal point present when appropriate) or "compact codes" (decimal point omitted).

Similarly for ICD-9-CM: "055" is a header for measles; 055.0, 055.1, 055.2, 055.8, and 055.9 are assignable. Codes 055.3–055.6 do not exist. Code 055.7 is a header because 055.71 and 055.72 exist.

Some codes change status across years. For example, ICD-9-CM 516.3 was assignable in fiscal years 1997–2011 for the CDC extracts (2006–2011 for CMS) and became a header in 2012–2015.

Value

A data.frame with one or more rows per input, including columns

See Also

Other ICD tools: icd_compact_to_full(), is_icd()


Synthetic Data

Description

Synthetic Data

Usage

mdcr

Format

mdcr is a data.frame with 4 columns. Each row is for one ICD code.

See Also

Other datasets: mdcr_longitudinal


Synthetic Longitudinal Data

Description

Synthetic Longitudinal Data

Usage

mdcr_longitudinal

Format

mdcr_longitudinal is a data.frame with 4 columns. The codes are expected to be treated as diagnostic codes. Warning: there are a few ICD-9 codes which could match to procedure codes.

See Also

Other datasets: mdcr


Summaries of Comorbidities

Description

Build summaries (counts and percentages) for each comorbidity and other summary statistics by method.

Usage

## S3 method for class 'medicalcoder_comorbidities'
summary(object, ...)

Arguments

object

a medicalcoder_comorbidities object generated by calling comorbidities()

...

additional parameters, not currently used

Details

If a medicalcoder_comorbidities object has been modified so that it no longer has the structure required by medicalcoder's summary methods, summary falls back to the next applicable summary method with a warning.

Value

Either a list or a data.frame.

Examples

pccc_v3.1_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "pccc_v3.1",
                flag.method = 'current',
                poa = 1L)
summary(pccc_v3.1_results)

charlson_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "charlson_quan2011",
                flag.method = 'current',
                poa = 1L)
summary(charlson_results)

elixhauser_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "elixhauser_ahrq2025",
                primarydx = 1L,
                flag.method = 'current',
                poa = 1L)
summary(elixhauser_results)


Summaries of Comorbidities with Subconditions

Description

Build summaries (counts and percentages) for each Pediatric Complex Chronic Condition (PCCC) condition and subcondition.

Usage

## S3 method for class 'medicalcoder_comorbidities_with_subconditions'
summary(object, ...)

Arguments

object

a medicalcoder_comorbidities_with_subconditions object generated by calling comorbidities() with subconditions = TRUE. This is currently only applicable to PCCC.

...

additional parameters, not currently used

Value

a data.frame with five columns.

  1. condition the primary condition

  2. subcondition the subcondition(s) within the condition. There will be a row where subcondition is NA which is used to report the count and percent_of_cohort for the condition overall.

  3. count the number of rows in object with the applicable condition and subcondition.

  4. percent_of_cohort: a numeric value within [0, 100] for the percent of rows in object with the flagged condition and subcondition.

  5. percent_of_those_with_condition: a numeric value within [0, 100] for the subset of rows in object with the primary condition and the flagged subcondition. Will be NA for the primary condition.

See Also

comorbidities(), vignette(topic = "pccc", package = "medicalcoder")

Examples

pccc_v3.1_subcondition_results <-
  comorbidities(data = mdcr,
                icd.codes = "code",
                id.vars = "patid",
                dx.var = "dx",
                method = "pccc_v3.1",
                flag.method = 'current',
                poa = 1L,
                subconditions = TRUE)
summary(pccc_v3.1_subcondition_results)