Skip to contents

Convenience wrapper over bin_numeric() using the standard survey age bands. Also tolerates messy inputs such as "25 years" by extracting the first run of digits.

Usage

recode_age(
  x,
  breaks = ezrsurvey_default("age_breaks"),
  labels = ezrsurvey_default("age_labels"),
  quiet = FALSE
)

Arguments

x

A numeric or character vector of ages.

breaks, labels

Override the default bands if needed; the defaults come from the age_breaks / age_labels options (see ezrsurvey_options()).

quiet

If FALSE (default), report answers that held no number and answers that fell outside the bands. Set TRUE to silence both.

Value

A factor of age-band labels, levels in band order.

Details

Ages are first passed through ensure_numeric(), so messy entries like "22 years" or "age: 31" are handled, then binned with bin_numeric() using left-closed bands. An answer with no number in it at all ("young", "prefer not to say") becomes NA and is counted in a message, so a column that was never numeric does not pass for a column of missing ages. The default bands come from the age_breaks / age_labels options, so you can set your study's standard cohorts once with ezrsurvey_options(age_breaks = ..., age_labels = ...) (or a profile) instead of passing them on every call. For generational cohorts (Gen Z, Millennial, ...) use recode_generation() instead.

Examples

recode_age(c("17", "22 years", "31", "47"))
#> [1] 17 or younger 22 to 25      30 to 34      35+          
#> Levels: 17 or younger 18 to 21 22 to 25 26 to 29 30 to 34 35+