ez.age: Function to calculate age from date of birth.

View source: R/basic.R

ez.ageR Documentation

Function to calculate age from date of birth.

Description

This function calculates age in days, months, or years from a date of birth to another arbitrary date. This returns a numeric vector in the specified units.

Usage

ez.age(dob, enddate = Sys.Date(), units = "years", precise = TRUE)

Arguments

dob

a vector of class Date representing the date of birth/start date

enddate

a vector of class Date representing the when the observation's age is of interest, defaults to current date.

units

character, which units of age should be calculated? allowed values are days, months, and years

precise

logical indicating whether or not to calculate with leap year and leap second precision. If F, returns an integer.

Value

A numeric vector of ages the same length as the dob vector (if dob or enddate is NA, return NA as well–I hacked this)

Author(s)

Jason P. Becker from package eeptools (sligthly modified by Jerry)

Source

This function was developed in part from this response on the R-Help mailing list.

See Also

See also difftime which this function uses and mimics some functionality but at higher unit levels. ez.date ez.is.date ez.is.date.like ez.age may be like the Excel funciton: YEARFRAC

Examples

x <- as.Date(c("2011-01-01", "1996-02-29"))
age(x[1],x[2], units = "years")  # 3.893151 18.731507
floor(age(x[1],x[2], units = "years")) # 3 18

a <- as.Date(seq(as.POSIXct('1987-05-29 018:07:00'), len=26, by="21 day"))
b <- as.Date(seq(as.POSIXct('2002-05-29 018:07:00'), len=26, by="21 day"))
age <- age(a, units='years')
age
age <- age(a, units='months')
age
age <- age(a, as.Date('2005-09-01'))
age

jerryzhujian9/ezmisc documentation built on March 9, 2024, 12:44 a.m.