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

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

Arguments

x

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

reference

reference date(s) (defaults to today), will be coerced with as.POSIXlt()

exact

a logical to indicate whether age calculation should be exact, i.e. with decimals. It divides the number of days of year-to-date (YTD) of x by the number of days in the year of reference (either 365 or 366).

na.rm

a logical to indicate whether missing values should be removed

...

parameters passed on to as.POSIXlt(), such as origin

Value

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

Details

Ages below 0 will be returned as NA with a warning. Ages above 120 will only give a warning.

Stable lifecycle


The lifecycle of this function is stable. In a stable function, major changes are unlikely. This means that the unlying code will generally evolve by adding new arguments; removing arguments or changing the meaning of existing arguments will be avoided.

If the unlying code needs breaking changes, they will occur gradually. For example, a parameter will be deprecated and first continue to work, but will emit an message informing you of the change. Next, typically after at least one newly released version on CRAN, the message will be transformed to an error.

Read more on our website!

On our website https://msberends.github.io/AMR/ you can find a comprehensive tutorial about how to conduct AMR analysis, the complete documentation of all functions and an example analysis using WHONET data. As we would like to better understand the backgrounds and needs of our users, please participate in our survey!

See also

To split ages into groups, use the age_groups() function.

Examples

# 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