Completes age groups by adding missing age groups, either within the age range from age_gt to age_lt or using the full age list from seer_std_ages. If the columns age_min or age_max are missing from the input data, separate_age_groups() is first called to expand the age group variable.

complete_age_groups(data, ..., age_gt = NULL, age_lt = NULL,
  age_group = age_group, fill = list(n = 0), include_unknown = FALSE,
  std_age_groups = fcds_const("age_group"))

Arguments

data

A data frame.

...

Column specification passed on to tidyr::complete(). Used to identify additional columns that should also be completed. Columns that aren't declared here and do not have a default fill value will have NA values in the resulting output.

age_gt

Youngest age (inclusive).

age_lt

Eldest age (inclusive).

age_group

Unquoted column name containing the age grouping.

fill

A named list that for each variable supplies a single value to use instead of NA for missing combinations.

include_unknown

Should the "Unknown" age group be included?

std_age_groups

Character vector containing expected (or standard) age groups.

See also

Examples

dplyr::tibble( age_group = c("10 - 14", "15 - 19", "25 - 29"), n = 10:12 ) %>% complete_age_groups(age_gt = 10, age_lt = 35)
#> # A tibble: 5 x 2 #> age_group n #> <ord> <dbl> #> 1 10 - 14 10 #> 2 15 - 19 11 #> 3 20 - 24 0 #> 4 25 - 29 12 #> 5 30 - 34 0
set.seed(42) # Create an example data frame with age_groups at several grouping levels tidyr::crossing( group = LETTERS[1:3], sub_group = paste(1:3), ) %>% # Add a column with equivalent levels to `sub_group` dplyr::mutate(sub_group_equal = letters[as.integer(sub_group)]) %>% # Add age groups for ages < 25 to each group level tidyr::crossing( age_group = fcds_const("age_group")[1:5] ) %>% # Remove 20% of the age_groups dplyr::sample_frac(0.80) %>% # Use complete_age_groups() to complete the grouping, # using tidyr::complete() syntax to specificy which additional columns # are completed. complete_age_groups(age_lt = 25, group, tidyr::nesting(sub_group, sub_group_equal))
#> # A tibble: 45 x 4 #> group sub_group sub_group_equal age_group #> <chr> <chr> <chr> <ord> #> 1 A 1 a 0 - 4 #> 2 A 1 a 5 - 9 #> 3 A 1 a 10 - 14 #> 4 A 1 a 15 - 19 #> 5 A 1 a 20 - 24 #> 6 A 2 b 0 - 4 #> 7 A 2 b 5 - 9 #> 8 A 2 b 10 - 14 #> 9 A 2 b 15 - 19 #> 10 A 2 b 20 - 24 #> # … with 35 more rows