Uses the code mapping file provided by UK Biobank (resource 592).

map_codes(
  codes,
  from,
  to,
  all_lkps_maps = NULL,
  codes_only = FALSE,
  standardise_output = TRUE,
  unrecognised_codes = "error",
  preferred_description_only = NULL,
  reverse_mapping = "error",
  col_filters = default_col_filters()
)

Arguments

codes

A character vector of codes to be mapped.

from

Coding system that codes belong to.

to

Coding system to map codes to.

all_lkps_maps

Either a named list of lookup and mapping tables (either data frames or tbl_dbi objects), or the path to a SQLite database containing these tables (see also build_all_lkps_maps() and all_lkps_maps_to_db()). If NULL, will attempt to connect to an SQLite database named 'all_lkps_maps.db' in the current working directory, or to a a SQLite database specified by an environmental variable named 'ALL_LKPS_MAPS_DB' (see here for how to set environment variables using a .Renviron file). The latter method will be used in preference.

codes_only

bool. If TRUE, return a character vector of unique codes. If FALSE (default), return a data frame of all results including code descriptions (useful for manual validation).

standardise_output

bool. If TRUE (default), outputs a data frame with columns named 'code', 'description' and 'code_type'. Otherwise returns a data frame with all columns for the relevant lookup sheet from (UK Biobank resource 592).

unrecognised_codes

Either 'error' (default) or 'warning'. If any input codes are unrecognised for the coding system being mapped from, then either an error or warning will be raised.

preferred_description_only

If TRUE, return only preferred descriptions for clinical codes with synonyms. Can only be TRUE if standardise_output is also TRUE. Default value is NULL.

reverse_mapping

If 'error' (default), an error raised if attempting to map between coding systems for which a mapping table does not exist. If 'warning', will raise a warning and attempt to use an existing mapping table in the opposite direction (for example, a mapping from ICD10 to Read 3 would be attempted using the Read 3-to-ICD10 mapping table).

col_filters

A named list where each name in the list refers to the name of a lookup or mapping table. Each item is also a named list, where the names refer to column names in the corresponding table, and the items are vectors of values to filter for. For example, list(my_lookup_table = list(colA = c("A", "B")) will result in my_lookup_table being filtered for rows where colA is either 'A' or 'B'. Uses default_col_filters() by default. Set to NULL to remove all filters.

Details

The values for arguments from and to must be one of read2, read3, icd9, icd10, bnf, dmd, read2_drugs or opcs4.

See also

Other Clinical code lookups and mappings: codes_starting_with(), default_col_filters(), get_mapping_df(), lookup_codes(), reformat_icd10_codes()

Examples

# build dummy all_lkps_maps
all_lkps_maps_dummy <- build_all_lkps_maps_dummy()

# map codes from Read 2 to ICD10
map_codes(
  codes = "G20..",
  from = "read2",
  to = "icd10",
  all_lkps_maps = all_lkps_maps_dummy
)
#> # A tibble: 1 × 3
#>   code  description                      code_type
#>   <chr> <chr>                            <chr>    
#> 1 I10X  Essential (primary) hypertension icd10