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/Rtmplwnqmn/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, 119, 153, 153, 209, 299, 335, 340, 340, 369, 369…
#> $ cohort_start_date <date> 2018-08-13, 1968-04-07, 2001-11-28, 2008-02-17, 198…
#> $ ethnicity         <chr> "Missing", "Missing", "Missing", "Missing", "Missing…
# }