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/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 |>
  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> 4246, 4268, 4270, 4333, 4392, 4398, 4401, 4474, 4475…
#> $ cohort_start_date <date> 1968-11-08, 1993-12-17, 2015-11-22, 1997-05-03, 201…
#> $ location          <chr> "Missing", "Missing", "Missing", "Missing", "Missing…
# }