Interpret regular expressions

  collapse = TRUE,
  comment = "#>"

Classcodes objects (as described in vignette("classcodes")) use regular expressions to classify/categorize individual codes into groups (i.e. comorbidity conditions). Those regular expressions might be hard to interpret on their own. Several methods are therefore available to aid such interpretation of the classcodes objects.


A graphical representation of a classcodes object is created by visualize(). It will be showed in the default web browser (requires an Internet connection; not available within this vignette).


Visualization of all groups (comorbidity conditions) simultaneously might lead to complex figures. We can focus on a specific group (comorbidity) by the group argument. How is r charlson$group[1] codified by regex_icd9cm_deyo?

visualize(charlson, "myocardial infarction", regex    = "icd9cm_deyo")

Hence, all ICD-9 codes starting with 41 followed by either 0 or 2 will be recognized as myocardial infarction according to icd9cm_deyo. The corresponding regular expression for ICD-10 is:

visualize(charlson, "myocardial infarction", regex = "icd10")

Such codes should start with I2 followed by either 1, 2 or 52. The vertical bar | (in the regular expression of the heading) indicates a logical "or". See ?regex for more details on how to use regular expressions in R (Perl-like versions are currently not allowed).


An alternative representation is to list all relevant codes identified by each regular expression. This is implemented by the summary() method for classcodes objects. Note, however, that the regular expressions are stand alone in each classcodes object. Hence, there are no static look-up-tables to map individual codes to each group. We therefore need to specify a code list/dictionary of all possible codes to be recognized by those regular expressions. Then summary() will categorize those and display the result. Common code lists are found in the decoder package and are accessed automatically through the coding argument to summary(). Hence, there is a "keyvalue" object icd10cm with all ICD-10-CM codes in {decoder}:


We can use this code list to identify all codes recognized by charlson with its default classification based on "icd10". The printed result (see ?print.summary.classcodes) is a tibble with each group and a comma separated code list.

s <- summary(charlson, coding = "icd10cm")

A list with all code vectors (to use for programmatic purposes) is also returned (invisible) and accessed by s$codes_vct.

Now, compare the result above with the output based on a different code list, namely ICD-10-SE, the Swedish version of ICD-10, instead of ICD-10-CM:

summary(charlson, coding = "icd10se")

There are some noticeable differences. AIDS/HIV for example has only one code deemed clinically relevant in the USA (thus included in the CM-version of ICD-10), although there are 22 different codes potentially used in the Swedish national patient register. There are additional differences concerning the fifth code position (digits in ICD-10-CM and characters in ICD-10-SE). Those mark national modifications to the original ICD-10 codes, which has only 4 positions (one character and three digits). For this example, the charlson$icd10 column was based on ICD-10-CM [@Quan2005]. The comparison above thus highlights potential differences when using this classification in a setting based on another classification (such as with data from the Swedish national patient register).

If we are interested in another code version, for example as specified by ICD-9-CM [@Deyo1992] , this can be specified by the regex-argument passed by the cc_args argument to the set_classcodes function. Simultaneously, the coding argument is set to icd9cmd to match the regular expressions to the disease part of ICD-9-CM classification.

  charlson, coding = "icd9cmd",
  cc_args = list(regex = "icd9cm_deyo")


Even with individual codes summarized, those might still be hard to interpret on their own. The decoder package can help to translate codes to readable names/description. This is facilitated by the codebook() function in the {coder} package.

The main purpose is to export an Excel-file (if path specified by argument file). The output is otherwise a list, including both a summary table (described above) and a tibble with "all_codes" explaining the meaning of each code.

We can compare the codes recognized as AIDS/HIV by either ICD-10-CM or ICD-10-SE:

cm <- codebook(charlson, "icd10cm")$all_codes
cm[cm$group == "AIDS/HIV", ]

se <- codebook(charlson, "icd10se")$all_codes
se[se$group == "AIDS/HIV", ]


Several codebooks can be combined (exported to a single Excel-file) by the function codebooks() (note the plural s). This is difficult to illustrate in a vignette but examples are provided in ?codebooks


Try the coder package in your browser

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

coder documentation built on Jan. 18, 2021, 5:07 p.m.