\

[Experimental]

age() computes the year age of a person considering a reference date point.

age(birth_date, reference_date = base::Sys.Date(), round = FALSE)

Arguments

birth_date

A Date object with

reference_date

(optional) a Date or POSIXt object indicating a reference date (default: base::Sys.Date()).

round

(optional) a logical value indicating if the the function must return a rounded year age.

Value

A numeric object with a year age.

Examples

## Scalar example

birth_date <- lubridate::as_date("2000-01-01")
reference_date <- lubridate::as_date("2020-01-01")
age(birth_date, reference_date, round = FALSE)
#> [1] 19.99722
#> [1] 19.99722 # Expected
age(birth_date, reference_date, round = TRUE)
#> [1] 20
#> [1] 20

# Note that some years are not equal to exactly 365 days.

## Vector example

birth_date <- lubridate::as_date(c(
    "1800-01-01", "1900-01-01", "2000-01-01"
    ))
reference_date <- lubridate::as_date("2020-01-01") # length == 1
age(birth_date, reference_date)
#> [1] 220.00000 120.00000  19.99722
#> [1] 220.00000 120.00000  19.99722 # Expected

birth_date <- lubridate::as_date(c(
    "1800-01-01", "1900-01-01", "2000-01-01"
    ))
reference_date <- lubridate::as_date(c( # same length as `birth_date`
    "1900-01-01", "2000-01-01", "2020-01-01"
    ))
age(birth_date, reference_date)
#> [1] 100.00000 100.00000  19.99722
#> [1] 100.00000 100.00000  19.99722 # Expected