age_breaks: Convert Ages or Dates, to a Factor Variable of Standard Age...

Description Usage Arguments Value Note Author(s) Examples

Description

Convert integer/numeric ages, or dates (of birth) to a factor of standard age groups, with presentable labels, ordered from youngest to oldest. The default provides the 'standard' age groups used by much of the market research industry. Custom age breaks can also be used, by passing a vector to the breaks argument.

Usage

1
2
age_breaks(x, breaks = c(-Inf, 18, 25, 35, 45, 55, 65, +Inf), right = FALSE,
  ...)

Arguments

x

A vector of numeric data (ages), or one of the DateTimeClasses for a date of birth

breaks

a A numeric vector of cutpoints

right

Passed internally to cut Internally passed to cut

...

Additional arguments passed to cut

Value

factor age groups

Note

This function does not round ages, and so the common cultural/numerical interpretation of age works with decimal numbers. For example someone who has existed for 17.999 years is said to be 17 years old. The function (with default settings) will process 17 and 17.9999 in the same fashion.

Author(s)

Brendan Rocks rocks.brendan@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data(test_data)

# A 'rough and ready' way of calculating ages
test_data$age <- as.numeric(Sys.Date() - test_data$dob) /365

# Let's put those ages into groups
test_data$age_group <- age_breaks(test_data$age)

# Hooray!
table(test_data$age_group)

# We could do the above by simply passing in the dates of birth
test_data$age_group <- age_breaks(test_data$dob)

brendan-R/brocks documentation built on May 13, 2019, 5:07 a.m.