calculate_age: Calculate age

calculate_ageR Documentation

Calculate age

Description

calculate_age will determine age in years based upon two comparison dates.

Usage

calculate_age(
  dob,
  age_day = Sys.Date(),
  units = "years",
  floor = TRUE,
  force_dates = TRUE
)

Arguments

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 "years"). Passed to duration.

floor

Boolean to determine to floor round the result (default set to TRUE).

force_dates

Force the dob and age.day parameter to be a date to avoid date-time vs date comparisons.

Details

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.

Value

Age in units. Will be an integer if floor = TRUE.

Note

Adapted from SO post. Earlier methods used as.integer((compare_date - dob) / 365.25)

Author(s)

Adapted from Gregor Thomas https://stackoverflow.com/users/903061/gregor-thomas>

Source

https://stackoverflow.com/questions/27096485/change-a-column-from-birth-date-to-age-in-r

Examples

## 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)

al-obrien/farrago documentation built on April 14, 2023, 6:20 p.m.