Skip to contents

Add the ethnicity of a person to a table

Usage

addEthnicity(
  x,
  from = c("ethnicity_concept_id", "ethnicity_source_concept_id", "race_concept_id",
    "race_source_concept_id"),
  nameStyle = "ethnicity",
  name = tableName(x),
  missingEthnicityValue = "Missing"
)

Arguments

x

A cdm_table containing a person identifier column named person_id or subject_id.

from

A character vector specifying the ethnicity sources to try, in priority order. The first source that provides a value is used. Available options are:

  • ethnicity_concept_id to assign ethnicity using the concept_name associated with the ethnicity_concept_id column of the person table.

  • ethnicity_source_concept_id to assign ethnicity using the concept_name associated with the ethnicity_source_concept_id column of the person table.

  • race_concept_id to assign ethnicity using the concept_name associated with the race_concept_id column of the person table.

  • race_source_concept_id to assign ethnicity using the concept_name associated with the race_source_concept_id column of the person table.

  • ethnicity_source_value to assign ethnicity using the value of the column ethnicity_source_value in the person table.

  • race_source_value to assign ethnicity using the value of the column race_source_value in the person table.

  • nhs-categories to assign ethnicity using NHS Ethnic Category.

  • nhs-groups to assign ethnicity using broad groups of NHS Ethnic Category as described in doi:10.1038/s41597-024-02958-1 .

nameStyle

A character string specifying the name of the new column.

name

A character string specifying the name of the output table. If NULL, a temporary table is created.

missingEthnicityValue

A character string used to replace missing ethnicity values.

Value

The table x with a new column containing the patient's ethnicity.

Examples

# \donttest{
library(omock)
library(duckdb)
library(OmopIndices)
library(dplyr)
library(CohortConstructor)

cdm <- mockCdmFromDataset(datasetName = "GiBleed", source = "duckdb")
#> ℹ Loading bundled GiBleed tables from package data.
#> ℹ Adding drug_strength table.
#> ℹ Creating local <cdm_reference> object.
#> ℹ Inserting <cdm_reference> into duckdb.
#> duckdb keeps downloaded extensions and secrets in a temporary directory:
#> ℹ /tmp/RtmpNEUAUi/duckdb
#> This is removed when the R session ends.
#> • Extensions are re-downloaded each session.
#> • Secrets are lost.
#> ℹ Run duckdb(shared_home = TRUE) (or create ~/.duckdb) to keep them (suitable for most users).
#> ℹ Run duckdb(shared_home = FALSE) to accept the temporary directory (and silence this message).
#> ℹ See ?duckdb_storage for details and alternatives.
cdm$cohort <- conceptCohort(
  cdm = cdm,
  conceptSet = list(sinusitis = c(257012L, 4283893L, 4294548L, 40481087L)),
  name = "cohort"
)
#> ℹ Subsetting table condition_occurrence using 4 concepts with domain:
#>   condition.
#> ℹ Combining tables.
#> ℹ Creating cohort attributes.
#> ℹ Applying cohort requirements.
#> ℹ Merging overlapping records.
#> ✔ Cohort cohort created.

cdm$cohort |>
  addEthnicity() |>
  select(subject_id, cohort_start_date, ethnicity) |>
  glimpse()
#> ℹ Trying to get ethnicity from: ethnicity_concept_id.
#> ✖ Ethnicity could not be added from ethnicity_concept_id.
#> ℹ Trying to get ethnicity from: ethnicity_source_concept_id.
#> ✖ Ethnicity could not be added from ethnicity_source_concept_id.
#> ℹ Trying to get ethnicity from: race_concept_id.
#> ✖ Ethnicity could not be added from race_concept_id.
#> ℹ Trying to get ethnicity from: race_source_concept_id.
#> ✖ Ethnicity could not be added from race_source_concept_id.
#> ! No ethnicity found, variable will be filled with `Missing`.
#> Rows: ??
#> Columns: 3
#> $ subject_id        <int> 19, 61, 119, 119, 140, 153, 153, 335, 348, 539, 539,…
#> $ cohort_start_date <date> 2011-02-25, 1980-09-09, 1960-08-27, 1991-04-11, 197…
#> $ ethnicity         <chr> "Missing", "Missing", "Missing", "Missing", "Missing…
# }