Skip to contents

Categorise continuous age into age groups

Usage

as_age_groups(
  var,
  min = 0,
  max = 120,
  by = 10,
  grouping_method = c("user_defined", "ons_1", "ons_2", "nhs_survey")
)

Arguments

var

Name of variable or vector

min

Numeric, specifying the minimum age of the first age group. Age values lower than this will be returned as missing values (NA)

max

Numeric, specifying the upper end of the last age group

by

Numeric, increment of the age categories

grouping_method

String, specifying the method to be used for grouping age into categories. Details about the different methods are available here: "user_defined", "ons_1", "ons_2", "nhs_survey" ... TODO

Examples

# Example using a vector:
set.seed(123)
age <- sample(1:100, 100, replace = TRUE)
as_age_groups(age)
#>   [1] 30-39 70-79 50-59 10-19 60-69 40-49 50-59 40-49 10-19 20-29 90-99 90-99
#>  [13] 60-69 90-99 50-59 90-99 0-9   90-99 90-99 70-79 20-29 0-9   40-49 0-9  
#>  [25] 80-89 30-39 70-79 80-89 40-49 70-79 10-19 30-39 0-9   0-9   40-49 70-79
#>  [37] 20-29 20-29 60-69 50-59 0-9   50-59 20-29 90-99 30-39 80-89 30-39 90-99
#>  [49] 60-69 70-79 70-79 60-69 10-19 80-89 90-99 90-99 20-29 30-39 20-29 70-79
#>  [61] 40-49 40-49 90-99 60-69 90-99 10-19 90-99 0-9   70-79 80-89 80-89 30-39
#>  [73] 30-39 80-89 50-59 30-39 0-9   10-19 60-69 20-29 50-59 20-29 80-89 30-39
#>  [85] 20-29 80-89 30-39 40-49 30-39 10-19 30-39 30-39 60-69 90-99 10-19 90-99
#>  [97] 90-99 70-79 60-69 20-29
#> 13 Levels: 0-9 10-19 20-29 30-39 40-49 50-59 60-69 70-79 80-89 ... 120+

# Example using a data frame
tibble::tibble(age = sample(1:115, 100, replace = TRUE)) %>%
  dplyr::mutate(age_groups = as_age_groups(age, min = 10, max = 100))
#> # A tibble: 100 × 2
#>      age age_groups
#>    <int> <fct>     
#>  1    79 70-79     
#>  2    85 80-89     
#>  3    37 30-39     
#>  4     8 NA        
#>  5    51 50-59     
#>  6    74 70-79     
#>  7    50 50-59     
#>  8   106 100+      
#>  9   108 100+      
#> 10    98 90-99     
#> # … with 90 more rows