View source: R/create_agegroups.R
create_agegroups | R Documentation |
Creates age groups from numeric values using customizable break points and formatting options. The function allows for flexible formatting and customization of age group labels.
If a factor is returned, this factor includes factor levels of unobserved age groups. This allows for reproducible age groups, which can be used for joining data (e.g. adding age grouped population numbers for incidence calculation).
create_agegroups(
values,
age_breaks = c(5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90),
breaks_as_lower_bound = TRUE,
first_group_format = "0-{x}",
interval_format = "{x}-{y}",
last_group_format = "{x}+",
pad_numbers = FALSE,
pad_with = "0",
collapse_single_year_groups = FALSE,
na_label = NA,
return_factor = FALSE
)
values |
Numeric vector of ages to be grouped |
age_breaks |
Numeric vector of break points for age groups. |
breaks_as_lower_bound |
Logical; if |
first_group_format |
Character string template for the first age group. Uses glue syntax. |
interval_format |
Character string template for intermediate age groups. Uses glue syntax. |
last_group_format |
Character string template for the last age group. Uses glue syntax. |
pad_numbers |
Logical or numeric; if numeric, pad numbers up to the specified length (Tip: use |
pad_with |
Character to use for padding numbers. Default: |
collapse_single_year_groups |
Logical; if |
na_label |
Label for |
return_factor |
Logical; if |
Vector of age group labels (character or factor depending on return_factor)
# Basic usage
create_agegroups(1:100)
# Custom formatting with upper bounds
create_agegroups(1:100,
breaks_as_lower_bound = FALSE,
interval_format = "{x} to {y}",
first_group_format = "0 to {x}"
)
# Ages 1 to 5 are kept as numbers by collapsing single year groups
create_agegroups(1:10,
age_breaks = c(1, 2, 3, 4, 5, 10),
collapse_single_year_groups = TRUE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.