# Adding table intersections

So far we’ve seen that we can add variables indicating intersections based on cohorts or concept sets. One additional option we have is to simply add an intersection based on a table.

Let’s again create a cohort containing people with an ankle sprain.

    library(CodelistGenerator)
    library(PatientProfiles)
    library(dplyr)
    library(CohortConstructor)
    library(ggplot2)
    library(omock)

    cdm <- mockCdmFromDataset(datasetName = "GiBleed", source = "duckdb")

    cdm$ankle_sprain <- conceptCohort(
      cdm = cdm,
      name = "ankle_sprain",
      conceptSet = list("ankle_sprain" = 81151)
    )
    cdm$ankle_sprain
    #> # Source:   table<results.test_ankle_sprain> [?? x 4]
    #> # Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #>    cohort_definition_id subject_id cohort_start_date cohort_end_date
    #>                   <int>      <int> <date>            <date>
    #>  1                    1       5117 2017-12-09        2017-12-30
    #>  2                    1       1158 1984-03-11        1984-03-25
    #>  3                    1       1535 1999-09-10        1999-10-01
    #>  4                    1       2649 1986-08-03        1986-08-31
    #>  5                    1       3728 1987-08-09        1987-08-23
    #>  6                    1       3067 1979-08-01        1979-08-15
    #>  7                    1       3079 2003-07-17        2003-07-31
    #>  8                    1       1496 1949-07-11        1949-08-15
    #>  9                    1       2343 2012-11-09        2012-11-23
    #> 10                    1       2351 2011-09-30        2011-10-14
    #> # ℹ more rows

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "condition_occurrence",
        window = c(-30, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id           <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                     <int> 4847, 3601, 1448, 5328, 2616, 1401, 369…
    #> $ cohort_start_date              <date> 1953-09-28, 1974-03-02, 1978-01-01, 19…
    #> $ cohort_end_date                <date> 1953-10-19, 1974-04-06, 1978-01-15, 19…
    #> $ condition_occurrence_m30_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

We can use table intersection functions to check whether someone had a record in the drug exposure table in the 30 days before their ankle sprain. If we set targetStartDate to “drug_exposure_start_date” and targetEndDate to “drug_exposure_end_date” we are checking whether an individual had an ongoing drug exposure record in the window.

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        targetStartDate = "drug_exposure_start_date",
        targetEndDate = "drug_exposure_end_date",
        window = c(-30, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    #> $ subject_id              <int> 1448, 4942, 564, 97, 480, 1335, 4026, 3047, 28…
    #> $ cohort_start_date       <date> 1978-01-01, 1998-04-16, 2006-08-19, 1992-09-0…
    #> $ cohort_end_date         <date> 1978-01-15, 1998-05-21, 2006-09-09, 1992-10-0…
    #> $ drug_exposure_m30_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

Meanwhile if we set we set targetStartDate to “drug_exposure_start_date” and targetEndDate to “drug_exposure_start_date” we will instead be checking whether they had a drug exposure record that started during the window.

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        window = c(-30, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    #> $ subject_id              <int> 1448, 4942, 564, 97, 480, 1335, 4026, 3047, 28…
    #> $ cohort_start_date       <date> 1978-01-01, 1998-04-16, 2006-08-19, 1992-09-0…
    #> $ cohort_end_date         <date> 1978-01-15, 1998-05-21, 2006-09-09, 1992-10-0…
    #> $ drug_exposure_m30_to_m1 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

As before, instead of a flag, we could also add count, date, or days variables.

    cdm$ankle_sprain |>
      addTableIntersectCount(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        window = c(-180, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 1448, 4942, 2945, 1334, 564, 2002, 97, 480, 1…
    #> $ cohort_start_date        <date> 1978-01-01, 1998-04-16, 1974-03-01, 1969-12-…
    #> $ cohort_end_date          <date> 1978-01-15, 1998-05-21, 1974-04-05, 1970-01-…
    #> $ drug_exposure_m180_to_m1 <dbl> 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, …

    cdm$ankle_sprain |>
      addTableIntersectDate(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        order = "last",
        window = c(-180, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 1448, 2945, 1334, 564, 2002, 97, 480, 1611, 4…
    #> $ cohort_start_date        <date> 1978-01-01, 1974-03-01, 1969-12-20, 2006-08-…
    #> $ cohort_end_date          <date> 1978-01-15, 1974-04-05, 1970-01-17, 2006-09-…
    #> $ drug_exposure_m180_to_m1 <date> 1977-11-23, 1973-11-18, 1969-11-02, 2006-07-…

    cdm$ankle_sprain |>
      addTableIntersectDate(
        tableName = "drug_exposure",
        indexDate = "cohort_start_date",
        order = "last",
        window = c(-180, -1)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id               <int> 1448, 2945, 1334, 564, 2002, 97, 480, 1611, 4…
    #> $ cohort_start_date        <date> 1978-01-01, 1974-03-01, 1969-12-20, 2006-08-…
    #> $ cohort_end_date          <date> 1978-01-15, 1974-04-05, 1970-01-17, 2006-09-…
    #> $ drug_exposure_m180_to_m1 <date> 1977-11-23, 1973-11-18, 1969-11-02, 2006-07-…

In these examples we’ve been adding intersections using the entire drug exposure concept table. However, we could have subsetted it before adding our table intersection. For example, let’s say we want to add a variable for acetaminophen use among our ankle sprain cohort. As we’ve seen before we could use a cohort or concept set for this, but now we have another option - subset the drug exposure table down to acetaminophen records and add a table intersection.

    acetaminophen_cs <- getDrugIngredientCodes(
      cdm = cdm,
      name = c("acetaminophen")
    )

    cdm$acetaminophen_records <- cdm$drug_exposure |>
      filter(drug_concept_id %in% !!acetaminophen_cs[[1]]) |>
      compute()

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "acetaminophen_records",
        indexDate = "cohort_start_date",
        targetStartDate = "drug_exposure_start_date",
        targetEndDate = "drug_exposure_end_date",
        window = c(-Inf, Inf)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id              <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
    #> $ subject_id                        <int> 5117, 1158, 1535, 2649, 3728, 3067, …
    #> $ cohort_start_date                 <date> 2017-12-09, 1984-03-11, 1999-09-10,…
    #> $ cohort_end_date                   <date> 2017-12-30, 1984-03-25, 1999-10-01,…
    #> $ acetaminophen_records_minf_to_inf <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

Beyond this table intersection provides a means if implementing a wide range of custom analyses. One more example to show this is provided below, where we check whether individuals have a measurement or procedure record on the date of their ankle sprain.

    cdm$proc_or_meas <- union_all(
      cdm$procedure_occurrence |>
        select("person_id",
          "record_date" = "procedure_date"
        ),
      cdm$measurement |>
        select("person_id",
          "record_date" = "measurement_date"
        )
    ) |>
      compute()

    cdm$ankle_sprain |>
      addTableIntersectFlag(
        tableName = "proc_or_meas",
        indexDate = "cohort_start_date",
        targetStartDate = "record_date",
        targetEndDate = "record_date",
        window = c(0, 0)
      ) |>
      glimpse()
    #> Rows: ??
    #> Columns: 5
    #> Database: DuckDB 1.4.0 [root@Darwin 24.6.0:R 4.4.1//private/var/folders/pl/k11lm9710hlgl02nvzx4z9wr0000gp/T/Rtmp9b2r2o/file10c2931d6453a.duckdb]
    #> $ cohort_definition_id <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    #> $ subject_id           <int> 5117, 1158, 1535, 2649, 3728, 3067, 3079, 1496, 2…
    #> $ cohort_start_date    <date> 2017-12-09, 1984-03-11, 1999-09-10, 1986-08-03, …
    #> $ cohort_end_date      <date> 2017-12-30, 1984-03-25, 1999-10-01, 1986-08-31, …
    #> $ proc_or_meas_0_to_0  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…