to_agegrp: Generate age-group from age

View source: R/misc_functions.R

to_agegrpR Documentation

Generate age-group from age

Description

'to_agegrp' creates a new column

Usage

to_agegrp(
  dtb,
  grp_width = 5L,
  max_age = 85L,
  age_colname = "age",
  agegrp_colname = "agegrp",
  to_factor = TRUE,
  min_age = NULL
)

Arguments

dtb

A data.table with a column named age.

grp_width

The group width for the age groups.

max_age

The max age for the closed age groups. For ages above the max age, an open age group will be created, named max_age+ (i.e. 85+).

age_colname

A string denoting the age column in dtb.

agegrp_colname

A string denoting the name of the column that will be created for age groups.

to_factor

A logical. If TRUE, then the age-groups column is converted to factor.

min_age

The minimum age for the age group. If 'NULL' the minimum will be considered the that that is not more than the minimum age in the data that can be divided with grp_width

Value

a data.table, invisibly.

Examples

library(data.table)
library(CKutils)
to_agegrp(data.table(age = 0:99))[]
to_agegrp(data.table(age = 0:99), max_age = 80L)[]
to_agegrp(data.table(age = 0:99), grp_width = 10, max_age = 85)[]

ChristK/CKutils documentation built on April 11, 2025, 10:11 p.m.