Learn R Programming

SciencesPo (version 1.02.12)

ageGroups: Return a factor variable of age groups

Description

Return a factor variable using breaks and labels. The function will calculate the age based upon the to if given, otherwise the age.var will be used.

Usage

ageGroups(x = NULL, from, to, breaks, labels)

Arguments

x
if a var containing the age already exists.
from
The date of origin, typically birthdate.
to
The up to date to compute the age.
breaks
The numeric break guide for grouping age.
labels
The labels for the age groups, can also be set to labels=NULL.

encoding

UTF-8

Examples

Run this code
# The age groupings used by IBGE (grandes grupos).
# simulate vector with 1000 age values
age <- sample(0:100, 1000, replace = TRUE)
mean(age); sd(age);
ageGroups(age, breaks = c(0, 14, 64, Inf), labels = NULL )
ageGroups(age, breaks = c(0, 14, 64, Inf),
labels = c("<14", "15-64", "65+") )

ibge_brks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, Inf)
ibge_lbls = c("0-4", "5-9", "10-14", "15-19", "20-24",
		"25-29", "30-34", "35-39", "40-44", "45-49", "50-54",
 	"55-59", "60-64", "65-69", "70+")
ageGroups(age, breaks = ibge_brks, labels = ibge_lbls )

Run the code above in your browser using DataLab