A function to recode values via named lookup tables (i.e call an epidm lookup table and recode where we are aware of a new value). It routes to a specific lookup based on type, returning a character vector where each input value has been mapped to its corresponding replacement. If a value is not found in the lookup then the original value is returned.
Built‑in lookups include:
species: Uses therespeciate_organismdataset to standardise and reclassify organism names (e.g., historic → current nomenclature). This supports consistent reporting across SGSS and other laboratory datasets.specimen: Uses thespecimen_type_groupingdataset to assign raw laboratory specimen types into harmonised specimen groups. This enables consistent grouping for reporting, aggregation, and filtering.genus_gram_stain: Uses thegenus_gram_stainlookup table, which provides Gram stain classifications by bacterial genus. This reference is manually maintained against the UKHSA SGSS database and supports rapid filtering and high‑level organism categorisation. Users should raise an issue or submit a pull request to theepidmGitHub repository if an organism/genus is missing.lab_data: Uses thelab_datalookup dataset for harmonising laboratory code systems and internal SGSS mappings, supporting standardised laboratory result interpretation within surveillance pipelines.inpatient_admission_method: Uses the internal lookup tableepidm:::group_inpatient_admission_methodto categorise raw hospital admission method codes into operationally meaningful groups.inpatient_discharge_destination: Uses the internal tableepidm:::group_inpatient_discharge_destinationto group hospital discharge destination codes into standardised categories for inpatient pathway analysis.ecds_destination_code: Uses the internal tableepidm:::group_ecds_discharge_destination, providing grouped mappings for ECDS (Emergency Care Data Set) discharge codes.manual: Allows the user to supply their own lookup through.import = list(new, old). This is useful when working with local, provisional, or evolving code sets not yet included in the package’s centralised lookup tables.
Usage
lookup_recode(
src,
type = c("species", "specimen", "inpatient_admission_method",
"inpatient_discharge_destination", "ecds_destination_code", "manual"),
.import = NULL
)Arguments
- src
Character vector (or column) of values to recode. Coerced to character if needed.
- type
Character scalar specifying the lookup to use. One of:
'species','specimen','inpatient_admission_method','inpatient_discharge_destination','ecds_destination_code','manual'.- .import
A two‑element list in the format
list(new, old)used only whentype = 'manual'. Each element must be a vector of equal length.
Value
A character vector containing the recoded values, aligned 1:1 with src.
Values not present in the lookup are returned unchanged.
Examples
df <- data.frame(
spec = c(
sample(grep(")",
respeciate_organism$previous_organism_name,
value=TRUE,
invert = TRUE),
9),
"ESCHERICHIA COLI","SARS-COV-2","CANDIDA AUREUS"),
type = sample(specimen_type_grouping$specimen_type,12),
date = sample(seq.Date(from = Sys.Date()-365,
to = Sys.Date(),
by = "day"),12)
)
df <- df[order(df$date),]
# show the data before the changes
df
#> spec type date
#> 6 ACTINOBACULUM SCHAALII RECTUM 2025-03-16
#> 1 STREPTOCOCCUS GROUP D SYNOVIAL FLUID 2025-04-30
#> 12 CANDIDA AUREUS TRACHEAL ASPIRATE 2025-05-22
#> 9 TETRATHIOBACTER KASHMIRENSIS DUODENUM 2025-07-30
#> 2 PROPIONIBACTERIUM ACNES BRONCHIAL 2025-07-31
#> 7 CHRYSEOBACTERIUM MIRICOLA PERIANAL 2025-08-03
#> 4 ALCALIGENES PIECHAUDII LOWER GENITAL TRACT 2025-08-12
#> 3 EUBACTERIUM AEROFACIENS MIDDLE EAR/MASTOID 2025-08-17
#> 10 ESCHERICHIA COLI PUS (SOURCE UNKNOWN) 2025-10-01
#> 8 LEPTOSPIRA ICTEROHAEMORRHAGIAE EMU 2025-11-10
#> 5 CORYNEBACTERIUM EQUI BIOPSY-NOS 2026-01-06
#> 11 SARS-COV-2 SPLEEN 2026-01-24
# check the lookup tables
# observe the changes
head(respeciate_organism[1:2])
#> previous_organism_name organism_species_name
#> 1 ALCALIGENES DENITRIFICANS ACHROMOBACTER DENITRIFICANS
#> 2 ALCALIGENES PIECHAUDII ACHROMOBACTER PIECHAUDII
#> 3 ALCALIGENES XYLOSOXIDANS ACHROMOBACTER XYLOSOXIDANS
#> 4 ALCALIGENES XYLOSOXIDANS XYLOSOXIDANS ACHROMOBACTER XYLOSOXIDANS
#> 5 ACTINOBACULUM SCHAALII ACTINOTIGNUM SCHAALII
#> 6 TETRATHIOBACTER KASHMIRENSIS ADVENELLA KASHMIRENSIS
df$species <- lookup_recode(df$spec,'species')
df[,c('spec','species','date')]
#> spec species date
#> 6 ACTINOBACULUM SCHAALII ACTINOTIGNUM SCHAALII 2025-03-16
#> 1 STREPTOCOCCUS GROUP D ENTEROCOCCUS FAECALIS 2025-04-30
#> 12 CANDIDA AUREUS CANDIDA AUREUS 2025-05-22
#> 9 TETRATHIOBACTER KASHMIRENSIS ADVENELLA KASHMIRENSIS 2025-07-30
#> 2 PROPIONIBACTERIUM ACNES CUTIBACTERIUM ACNES 2025-07-31
#> 7 CHRYSEOBACTERIUM MIRICOLA ELIZABETHKINGIA MIRICOLA 2025-08-03
#> 4 ALCALIGENES PIECHAUDII ACHROMOBACTER PIECHAUDII 2025-08-12
#> 3 EUBACTERIUM AEROFACIENS COLLINSELLA AEROFACIENS 2025-08-17
#> 10 ESCHERICHIA COLI ESCHERICHIA COLI 2025-10-01
#> 8 LEPTOSPIRA ICTEROHAEMORRHAGIAE LEPTOSPIRA INTERROGANS 2025-11-10
#> 5 CORYNEBACTERIUM EQUI RHODOCOCCUS EQUI 2026-01-06
#> 11 SARS-COV-2 SARS-COV-2 2026-01-24
head(specimen_type_grouping)
#> specimen_type specimen_group
#> 1 BLOOD Blood
#> 2 CORD BLOOD Blood
#> 3 JOINT Bones & Joints
#> 4 SYNOVIAL FLUID Bones & Joints
#> 5 BONE Bones & Joints
#> 6 JOINT PROSTHESIS Bones & Joints
df$grp <- lookup_recode(df$type,'specimen')
df[,c('species','type','grp','date')]
#> species type grp
#> 6 ACTINOTIGNUM SCHAALII RECTUM Faeces & Lowergut
#> 1 ENTEROCOCCUS FAECALIS SYNOVIAL FLUID Bones & Joints
#> 12 CANDIDA AUREUS TRACHEAL ASPIRATE URT/Mouth/Ear
#> 9 ADVENELLA KASHMIRENSIS DUODENUM Faeces & Lowergut
#> 2 CUTIBACTERIUM ACNES BRONCHIAL Lower Respiratory Tract
#> 7 ELIZABETHKINGIA MIRICOLA PERIANAL Faeces & Lowergut
#> 4 ACHROMOBACTER PIECHAUDII LOWER GENITAL TRACT Genital
#> 3 COLLINSELLA AEROFACIENS MIDDLE EAR/MASTOID URT/Mouth/Ear
#> 10 ESCHERICHIA COLI PUS (SOURCE UNKNOWN) Fluids
#> 8 LEPTOSPIRA INTERROGANS EMU Urine/Kidney
#> 5 RHODOCOCCUS EQUI BIOPSY-NOS Swabs-General
#> 11 SARS-COV-2 SPLEEN Tissues
#> date
#> 6 2025-03-16
#> 1 2025-04-30
#> 12 2025-05-22
#> 9 2025-07-30
#> 2 2025-07-31
#> 7 2025-08-03
#> 4 2025-08-12
#> 3 2025-08-17
#> 10 2025-10-01
#> 8 2025-11-10
#> 5 2026-01-06
#> 11 2026-01-24
# for a tidyverse use
# df %>% mutate(spec=lookup_recode(spec,'species))
# manual input of your own lookup
# .import=list(new,old)
lookup_recode(
"ALCALIGENES DENITRIFICANS",
type = 'manual',
.import=list(respeciate_organism$organism_species_name,
respeciate_organism$previous_organism_name)
)
#> [1] "ACHROMOBACTER DENITRIFICANS"
