Skip to contents

Creates a cohort from a data frame by specifying ID and optional weight columns. Can handle duplicate IDs when deduplicate = TRUE.

Usage

create_cohort(data, id_col, weight_col = NULL, deduplicate = FALSE)

Arguments

data

A data.frame containing the data.

id_col

Quoted or unquoted id column name.

weight_col

Quoted or unquoted weight column name. Defaults to NULL, which assigns equal weights of 1.

deduplicate

Logical. If TRUE, allows duplicate IDs and deduplicates by taking distinct ID-weight pairs. Errors if duplicate IDs have different weights. Defaults to FALSE.

Value

A tibble representing a cohort with id and weight columns.

Details

Create a Cohort

Examples

# Create a cohort for US respondents
us_cohort <- example_data |>
  dplyr::filter(country == "US") |>
  create_cohort(
    id_col = response_id,
    weight_col = weight
  )

# Create a cohort without weights (equal weights assigned)
young_cohort <- example_data |>
  dplyr::filter(age_group == "18-24") |>
  create_cohort(
    id_col = response_id
  )
#> ℹ No weights provided. Using equal weights of 1.