calculate_age | R Documentation |
calculate_age
will determine age in years based upon two comparison dates.
calculate_age(
dob,
age_day = Sys.Date(),
units = "years",
floor = TRUE,
force_dates = TRUE
)
dob |
Vector containing date of birth for age calculation. |
age_day |
Reference date to calculate age as of that time-point. |
units |
unit to measure age (default set to |
floor |
Boolean to determine to floor round the result (default set to |
force_dates |
Force the dob and age.day parameter to be a date to avoid date-time vs date comparisons. |
Rounding is performed with floor
so you are the same age up to the day before the next birthday
(e.g 5 years old from 5th birthday through the day before your 6th birthday). Method uses the lubridate
package to
calculate the period between the two dates in "clock time", this ensures dates sharing the same month and day will calculate age in years
as expected (i.e. a duration counted in seconds may not count years with the same resolution). Set floor = FALSE
to
return decimal ages, and change units
for units other than years. Try combining with create_breaks
to make age groupings.
Age in units
. Will be an integer if floor = TRUE
.
Adapted from SO post. Earlier methods used as.integer((compare_date - dob) / 365.25)
Adapted from Gregor Thomas https://stackoverflow.com/users/903061/gregor-thomas>
https://stackoverflow.com/questions/27096485/change-a-column-from-birth-date-to-age-in-r
## Not run:
tempData <- data.frame(date_of_birth = as.Date(c('1991-01-01', '1990-02-04', '1991-03-14')), death_date = as.Date(c('1992-01-1', '2020-01-01', '1999-03-14')))
calculate_age(tempData$date_of_birth, tempData$death_date)
calculate_age(tempData$date_of_birth, tempData$death_date, units = 'minutes')
calculate_age(tempData$date_of_birth, tempData$death_date, floor = FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.