age(birth_date, reference_date = base::Sys.Date(), round = FALSE)A Date object with
(optional) a Date or
POSIXt object indicating a reference date
(default: base::Sys.Date()).
(optional) a logical value indicating if the
the function must return a rounded year age.
A numeric object with a year age.
## 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