create_age_groups: Create age groups

View source: R/create_age_groups.R

create_age_groupsR Documentation

Create age groups

Description

create_age_groups() takes a numeric vector and assigns each age to the appropriate age group.

Usage

create_age_groups(x, from = 0, to = 90, by = 5, as_factor = FALSE)

Arguments

x

a vector of numeric values

from

the start of the smallest age group. The default is 0.

to

the end point of the age groups. The default is 90.

by

the size of the age groups. The default is 5.

as_factor

The default behaviour is to return a character vector. Use TRUE to return a factor vector instead.

Details

The from, to and by values are used to create distinct age groups. from dictates the starting age of the lowest age group, and by indicates how wide each group should be. to stipulates the cut-off point at which all ages equal to or greater than this value should be categorised together in a ⁠to+⁠ group. If the specified value of to is not a multiple of by, the value of to is rounded down to the nearest multiple of by.

The default values of from, to and by correspond to the European Standard Population age groups.

Value

A character vector, where each element is the age group for the corresponding element in x. If as_factor = TRUE, a factor vector is returned instead.

Examples

age <- c(54, 7, 77, 1, 26, 101)

create_age_groups(age)
create_age_groups(age, from = 0, to = 80, by = 10)

# Final group may start below 'to'
create_age_groups(age, from = 0, to = 65, by = 10)

# To get the output as a factor:
create_age_groups(age, as_factor = TRUE)

Health-SocialCare-Scotland/phsmethods documentation built on Jan. 28, 2024, 3:18 a.m.