Skip to contents

Generates a single-year age variable from an age-group variable. For closed age groups, ages are sampled uniformly from the specified age range. For open-ended age groups (for example, "85 years and over"), ages are sampled uniformly between the lower age bound and max_age.

Usage

add_age_years(population, age_group, age = "age", max_age = 100L)

Arguments

population

A data.table containing population records.

age_group

Character scalar naming the age-group variable.

age

Character scalar naming the output age variable. Defaults to "age".

max_age

Integer specifying the maximum age used when sampling from open-ended age groups. Defaults to 100L.

Value

A data.table with an additional single-year age column.

Examples

population_dt <- data.table::data.table(
  AgeGroup = c(
    rep("25-34 years", 5),
    rep("85 years and over", 5)
  )
)

add_age_years(
  population_dt,
  age_group = "AgeGroup"
)
#>              AgeGroup   age
#>                <char> <int>
#>  1:       25-34 years    31
#>  2:       25-34 years    29
#>  3:       25-34 years    30
#>  4:       25-34 years    28
#>  5:       25-34 years    30
#>  6: 85 years and over    93
#>  7: 85 years and over    89
#>  8: 85 years and over    89
#>  9: 85 years and over    99
#> 10: 85 years and over    92