Skip to contents

These functions perform set operations on cohort objects.

  • intersect(x, y): Creates a new cohort containing only respondents present in both x and y.

  • union(x, y): Creates a new cohort containing all respondents from both x and y.

  • setdiff(x, y): Creates a new cohort containing respondents present in x but not in y.

If a respondent has different weights in both cohorts (for intersect or union), the weight from the first cohort (x) is used, and a warning is issued.

Usage

intersect(x, y, ...)

union(x, y, ...)

setdiff(x, y, ...)

# S3 method for class 'cohort'
intersect(x, y, ...)

# S3 method for class 'cohort'
union(x, y, ...)

# S3 method for class 'cohort'
setdiff(x, y, ...)

Arguments

x, y

A pair of cohort objects.

...

Additional arguments passed to methods.

Value

A new cohort object containing respondents based on the specified set operation.

Base functions

intersect(), union(), and setdiff() are generic functions from the generics package. This package provides methods for cohort objects.

Examples

if (FALSE) { # \dontrun{
us_cohort <- example_data |>
  dplyr::filter(country == "US") |>
  create_cohort(id_col = response_id, weight_col = weight)

young_cohort <- example_data |>
  dplyr::filter(age_group == "18-24") |>
  create_cohort(id_col = response_id, weight_col = weight)

# Intersect
young_us_cohort <- intersect(us_cohort, young_cohort)

# Union
all_cohort <- union(us_cohort, young_cohort)

# Set difference
us_not_young <- setdiff(us_cohort, young_cohort)
} # }