This function helps to recode age groups into new groups that are equal to or overlap the original age grouping.

recode_age_groups(data, breaks, age_group = age_group)

Arguments

data

A data frame, containin age group labels in the age_group column.

breaks

The breaks at which the lower bounds of age groups should be defined. If the first and last elements are NA, the lower bound is learned from the age groups present in the data. (Both, one, or none can be NA). If the first element is not missing, the lowest group will be ages 0 to min(breaks) - 1. If the last element is not missing, the highest group will be max(breaks) and above. Missing values within the breaks other than the first and last elements are ignored. The order of the breaks is not important beyond the presence of missing values at the extremes.

age_group

Unquoted column name containing the age grouping.

See also

Examples

d_age_groups <- dplyr::tibble( age_min = seq(0, 25, 5), age_max = seq(4, 29, 5) ) %>% format_age_groups() d_age_groups
#> # A tibble: 6 x 3 #> age_min age_max age_group #> <dbl> <dbl> <chr> #> 1 0 4 0 - 4 #> 2 5 9 5 - 9 #> 3 10 14 10 - 14 #> 4 15 19 15 - 19 #> 5 20 24 20 - 24 #> 6 25 29 25 - 29
recode_age_groups(d_age_groups, breaks = c(10, 20, 25))
#> # A tibble: 6 x 3 #> age_min age_max age_group #> <dbl> <dbl> <ord> #> 1 0 9 0 - 9 #> 2 0 9 0 - 9 #> 3 10 19 10 - 19 #> 4 10 19 10 - 19 #> 5 20 24 20 - 24 #> 6 25 Inf 25+
# Use maximum age in data for upper bound of highest group recode_age_groups(d_age_groups, breaks = c(10, 20, 25, NA))
#> # A tibble: 6 x 3 #> age_min age_max age_group #> <dbl> <dbl> <ord> #> 1 0 9 0 - 9 #> 2 0 9 0 - 9 #> 3 10 19 10 - 19 #> 4 10 19 10 - 19 #> 5 20 24 20 - 24 #> 6 25 29 25 - 29
# Use minimum age in data for lower bound of lowest group d_age_groups %>% dplyr::filter(age_min >= 5) %>% recode_age_groups(breaks = c(NA, 10, 20, 25))
#> # A tibble: 5 x 3 #> age_min age_max age_group #> <dbl> <dbl> <ord> #> 1 5 9 5 - 9 #> 2 10 19 10 - 19 #> 3 10 19 10 - 19 #> 4 20 24 20 - 24 #> 5 25 Inf 25+
# Dichotomize recode_age_groups(d_age_groups, breaks = 20)
#> # A tibble: 6 x 3 #> age_min age_max age_group #> <dbl> <dbl> <ord> #> 1 0 19 0 - 19 #> 2 0 19 0 - 19 #> 3 0 19 0 - 19 #> 4 0 19 0 - 19 #> 5 20 Inf 20+ #> 6 20 Inf 20+
d_age_groups %>% dplyr::filter(age_min >= 5) %>% recode_age_groups(breaks = c(NA, 20, NA))
#> # A tibble: 5 x 3 #> age_min age_max age_group #> <dbl> <dbl> <ord> #> 1 5 19 5 - 19 #> 2 5 19 5 - 19 #> 3 5 19 5 - 19 #> 4 20 29 20 - 29 #> 5 20 29 20 - 29