Creates a set of cohorts by grouping respondents based on specified columns. For each grouping column, separate cohorts are created for each unique value. For example, if grouping by country and customer status, there will be cohorts for each country and separate cohorts for each customer status, but not cohorts for each country-status combination. Optionally includes an overall population cohort.
Arguments
- data
A data.frame containing respondent data.
- id_col
Quoted or unquoted id column name containing unique respondent identifiers.
- grp_cols
A tidyselect expression to select grouping columns. Defaults to
NULL.- population
Whether to add the overall population cohort. Defaults to
TRUE.- weight_col
Quoted or unquoted weight column name. Defaults to
NULL, which assigns equal weights of 1.
Value
A tibble representing a cohort set with nested cohort objects. Each row contains:
column: Name of the grouping column
level: Value within that grouping column
cohort: A nested cohort object containing respondent IDs and weights
Examples
# Create cohorts by country and customer status
cohort_set <- example_data |>
create_cohort_set(
id_col = response_id,
grp_cols = c(country, customer),
population = TRUE,
weight_col = weight
)
# View the number of respondents in each cohort
cohort_set |>
dplyr::select(column, level, n, weighted_n)
#> Cohort Set
#> # A tibble: 7 × 4
#> column level n weighted_n
#> <chr> <chr> <int> <dbl>
#> 1 population population 500 503.
#> 2 country France 50 54.0
#> 3 country Germany 100 95.4
#> 4 country UK 160 165.
#> 5 country US 190 189.
#> 6 customer No 188 188.
#> 7 customer Yes 312 315.
# Create cohorts only by income level, without population cohort
income_cohorts <- example_data |>
create_cohort_set(
id_col = response_id,
grp_cols = income,
population = FALSE,
weight_col = weight
)