Skip to contents

Add the location to a table

Usage

addLocation(
  x,
  from = c("location_id", "care_site_id"),
  nameStyle = "location",
  name = tableName(x),
  locationSource = "location_source_value",
  missingLocationValue = "Missing"
)

Arguments

x

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

from

A character vector specifying the location sources to try, in priority order. The first source that provides a location is used. Supported values are location_id and care_site_id.

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.

locationSource

A character string specifying the column to retrieve from the location table. Supported values include location_source_value, city, state, zip, county, country_concept_id, and country_source_value.

missingLocationValue

A character string used to replace missing location values.

Value

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

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 |>
  addLocation() |>
  select(subject_id, cohort_start_date, location) |>
  glimpse()
#> ℹ Trying to get location from: location_id.
#> ✖ Location could not be added from location_id.
#> ℹ Trying to get location from: care_site_id.
#> ✖ Location could not be added from care_site_id.
#> ! No location found, variable will be filled with `Missing`.
#> Rows: ??
#> Columns: 3
#> $ subject_id        <int> 84, 86, 187, 187, 297, 297, 363, 380, 406, 502, 502,…
#> $ cohort_start_date <date> 2007-02-05, 2013-12-18, 1985-11-24, 2013-10-17, 200…
#> $ location          <chr> "Missing", "Missing", "Missing", "Missing", "Missing…
# }