Calculates age in years based on a reference date, which is the sytem date at default.

age(x, reference = Sys.Date(), exact = FALSE)

Arguments

x

date(s), will be coerced with as.POSIXlt

reference

reference date(s) (defaults to today), will be coerced with as.POSIXlt and cannot be lower than x

exact

a logical to indicate whether age calculation should be exact, i.e. with decimals

Value

An integer (no decimals) if exact = FALSE, a double (with decimals) otherwise

Read more on our website!

On our website https://msberends.gitlab.io/AMR you can find a tutorial about how to conduct AMR analysis, the complete documentation of all functions (which reads a lot easier than here in R) and an example analysis using WHONET data.

See also

age_groups to split age into age groups

Examples

# NOT RUN {
# 10 random birth dates
df <- data.frame(birth_date = Sys.Date() - runif(10) * 25000)
# add ages
df$age <- age(df$birth_date)
# add exact ages
df$age_exact <- age(df$birth_date, exact = TRUE)

df
# }