Skip to contents

exitAtFirstDate() resets cohort end date based on a set of specified column dates. The first date that occurs is chosen.

Usage

exitAtFirstDate(
  cohort,
  dateColumns,
  cohortId = NULL,
  returnReason = TRUE,
  name = tableName(cohort)
)

Arguments

cohort

A cohort table in a cdm reference.

dateColumns

Character vector indicating date columns in the cohort table to consider.

cohortId

Vector identifying which cohorts to modify (cohort_definition_id or cohort_name). If NULL, all cohorts will be used; otherwise, only the specified cohorts will be modified, and the rest will remain unchanged.

returnReason

If TRUE it will return a column indicating which of the dateColumns was used.

name

Name of the new cohort table created in the cdm object.

Value

The cohort table.

Examples

# \donttest{
library(CohortConstructor)
cdm <- mockCohortConstructor(tables = list(
"cohort" = dplyr::tibble(
  cohort_definition_id = 1,
  subject_id = c(1, 2, 3, 4),
  cohort_start_date = as.Date(c("2000-06-03", "2000-01-01", "2015-01-15", "2000-12-09")),
  cohort_end_date = as.Date(c("2001-09-01", "2001-01-12", "2015-02-15", "2002-12-09")),
  date_1 = as.Date(c("2001-08-01", "2001-01-01", "2015-01-15", "2002-12-09")),
  date_2 = as.Date(c("2001-08-01", NA, "2015-04-15", "2002-12-09"))
)
))
#> Warning: ! 9 column in cdm_source do not match expected column type:
#>  `cdm_source_abbreviation` is logical but expected character
#>  `cdm_holder` is logical but expected character
#>  `source_description` is logical but expected character
#>  `source_documentation_reference` is logical but expected character
#>  `cdm_etl_reference` is logical but expected character
#>  `source_release_date` is logical but expected date
#>  `cdm_release_date` is logical but expected date
#>  `cdm_version` is numeric but expected character
#>  `vocabulary_version` is logical but expected character
#> Warning: ! 3 column in concept do not match expected column type:
#>  `concept_id` is numeric but expected integer
#>  `valid_start_date` is character but expected date
#>  `valid_end_date` is character but expected date
#> Warning: ! 1 column in vocabulary do not match expected column type:
#>  `vocabulary_concept_id` is numeric but expected integer
#> Warning: ! 5 column in concept_relationship do not match expected column type:
#>  `concept_id_1` is numeric but expected integer
#>  `concept_id_2` is numeric but expected integer
#>  `valid_start_date` is logical but expected date
#>  `valid_end_date` is logical but expected date
#>  `invalid_reason` is logical but expected character
#> Warning: ! 2 column in concept_synonym do not match expected column type:
#>  `concept_id` is numeric but expected integer
#>  `language_concept_id` is numeric but expected integer
#> Warning: ! 4 column in concept_ancestor do not match expected column type:
#>  `ancestor_concept_id` is numeric but expected integer
#>  `descendant_concept_id` is numeric but expected integer
#>  `min_levels_of_separation` is numeric but expected integer
#>  `max_levels_of_separation` is numeric but expected integer
#> Warning: ! 9 column in drug_strength do not match expected column type:
#>  `drug_concept_id` is numeric but expected integer
#>  `ingredient_concept_id` is numeric but expected integer
#>  `amount_unit_concept_id` is numeric but expected integer
#>  `numerator_unit_concept_id` is numeric but expected integer
#>  `denominator_value` is logical but expected numeric
#>  `denominator_unit_concept_id` is numeric but expected integer
#>  `box_size` is numeric but expected integer
#>  `valid_start_date` is character but expected date
#>  `valid_end_date` is character but expected date
cdm$cohort |> exitAtFirstDate(dateColumns = c("date_1", "date_2"))
#> # Source:   table<main.cohort> [4 x 11]
#> # Database: DuckDB v1.1.2 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
#>   cohort_definition_id subject_id cohort_start_date cohort_end_date
#>                  <int>      <int> <date>            <date>         
#> 1                    1          1 2000-06-03        2001-08-01     
#> 2                    1          2 2000-01-01        2001-01-01     
#> 3                    1          4 2000-12-09        2002-12-09     
#> 4                    1          3 2015-01-15        2015-01-15     
#> # ℹ 7 more variables: cohort_definition_name <chr>,
#> #   cohort_definition_description <chr>, definition_type_concept_id <int>,
#> #   cohort_definition_syntax <chr>, subject_concept_id <int>,
#> #   cohort_initiation_date <date>, exit_reason <chr>
# }