Skip to contents

addCohortEventDays() finds the first or last event in each window. When no event is observed before the applicable boundary, the event is reported as "end_of_observation" if the observation period boundary is reached or "censor" if the window boundary or censorDate is reached. The days value represents that boundary.

Usage

addCohortEventDays(
  x,
  targetCohortTable,
  targetCohortId = NULL,
  indexDate = "cohort_start_date",
  censorDate = NULL,
  targetDate = "cohort_start_date",
  order = "first",
  window = c(0, Inf),
  multipleEvents = NULL,
  nameStyle = "{value}_{window_name}",
  name = NULL,
  type = "numeric"
)

Arguments

x

A table containing individuals in a CDM reference.

targetCohortTable

Name of the cohort table to intersect with.

targetCohortId

Cohort definition IDs to include from targetCohortTable. If NULL, all cohorts are included.

indexDate

Name of a date column in x, or a single date to use for all rows, used as the reference date.

censorDate

Date or name of a date column in x on which to censor follow-up. If NULL, no censoring is applied.

targetDate

Name or names of date columns in the target tables to use for the intersection.

order

Which record to use when multiple records occur in a window: "first" or "last".

window

Window or windows of time relative to indexDate to consider.

multipleEvents

How events occurring on the same date are handled. If NULL, the first event in the original event order is returned. If TRUE, all simultaneous event names are sorted alphabetically and joined with "; ". A character vector gives priority to the specified event names; the first matching name is returned, with unspecified names following in alphabetical order. Boundary labels ("censor" and "end_of_observation") are never combined with event names.

nameStyle

Naming pattern for the added columns. It must contain {value} and can also contain {window_name}.

name

Name of the new table. If NULL, a temporary table is returned.

type

Type of the created column(s). Counts, days, age, and observation durations can be "numeric" or "integer". Flag columns can also be "logical". Field columns can use "auto" to preserve the source type, or can be converted to "numeric", "integer", "logical", or "character".

Value

x with an event column and a days column of the requested type, relative to indexDate, for every window.

Examples

# \donttest{
library(PatientProfiles)

cdm <- mockPatientProfiles(source = "duckdb")

cdm$cohort1 |>
  addCohortEventDays(targetCohortTable = "cohort2")
#> # A query:  ?? x 6
#> # Database: DuckDB 1.5.4 [unknown@Linux 6.17.0-1020-azure:R 4.6.1/:memory:]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date
#>                   <int>      <int> <date>            <date>         
#>  1                    2          1 1952-11-14        1958-12-23     
#>  2                    1          3 1980-06-26        1982-11-16     
#>  3                    3          7 1979-10-30        1999-06-15     
#>  4                    2          9 1937-01-22        1943-04-25     
#>  5                    3         10 1970-10-31        1986-01-30     
#>  6                    1          2 1960-02-04        1961-05-14     
#>  7                    1          4 1981-01-26        1986-01-02     
#>  8                    3          5 1958-04-05        1960-05-24     
#>  9                    2          6 1930-11-02        1939-08-05     
#> 10                    1          8 2002-02-12        2002-02-22     
#> # ℹ 2 more variables: event_0_to_inf <chr>, days_0_to_inf <dbl>
# }