Computes the exact age in full years at the time of an event (e.g., diagnosis, death, or survey) by comparing a person's birth date to the date of the event.
calc_age(birth_date, onset_date)
A numeric vector of ages in years, with the same length
as birth_date
and onset_date
.
This function calculates age in completed years, taking into account
whether the birthday has occurred before the event date in the given year.
If birth_date
or onset_date
contains NA
,
the corresponding result will be NA
.
# Generate random birth dates
set.seed(123)
sdate <- as.Date("1960-01-01")
edate <- as.Date("1980-12-31")
bdate <- sample(seq(sdate, edate, by = "1 day"), 100, replace = TRUE)
# Generate random event dates
sdate <- as.Date("2020-01-01")
edate <- as.Date("2023-07-08")
event <- sample(seq(sdate, edate, by = "1 day"), 100, replace = TRUE)
# Calculate ages
ages <- calc_age(bdate, event)
head(ages)
#> [1] 54 55 54 60 48 54
# Handle missing values
bdate[1] <- NA
event[2] <- NA
calc_age(bdate, event)[1:5]
#> [1] NA NA 54 60 48