These functions perform set operations on cohort objects.
intersect(x, y): Creates a new cohort containing only respondents present in bothxandy.union(x, y): Creates a new cohort containing all respondents from bothxandy.setdiff(x, y): Creates a new cohort containing respondents present inxbut not iny.
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, ...)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)
} # }