AMR/man/age.Rd

49 lines
1.8 KiB
Plaintext
Raw Normal View History

2018-12-15 22:40:07 +01:00
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/age.R
\name{age}
\alias{age}
\title{Age in Years of Individuals}
2018-12-15 22:40:07 +01:00
\usage{
age(x, reference = Sys.Date(), exact = FALSE, na.rm = FALSE, ...)
2018-12-15 22:40:07 +01:00
}
\arguments{
2021-05-23 23:11:16 +02:00
\item{x}{date(s), \link{character} (vectors) will be coerced with \code{\link[=as.POSIXlt]{as.POSIXlt()}}}
2018-12-15 22:40:07 +01:00
2021-05-23 23:11:16 +02:00
\item{reference}{reference date(s) (defaults to today), \link{character} (vectors) will be coerced with \code{\link[=as.POSIXlt]{as.POSIXlt()}}}
2021-05-12 18:15:03 +02:00
\item{exact}{a \link{logical} to indicate whether age calculation should be exact, i.e. with decimals. It divides the number of days of \href{https://en.wikipedia.org/wiki/Year-to-date}{year-to-date} (YTD) of \code{x} by the number of days in the year of \code{reference} (either 365 or 366).}
2021-05-12 18:15:03 +02:00
\item{na.rm}{a \link{logical} to indicate whether missing values should be removed}
2020-12-22 00:51:17 +01:00
\item{...}{arguments passed on to \code{\link[=as.POSIXlt]{as.POSIXlt()}}, such as \code{origin}}
2018-12-15 22:40:07 +01:00
}
\value{
2020-03-07 21:48:21 +01:00
An \link{integer} (no decimals) if \code{exact = FALSE}, a \link{double} (with decimals) otherwise
2018-12-15 22:40:07 +01:00
}
\description{
2022-08-21 16:37:20 +02:00
Calculates age in years based on a reference date, which is the system date at default.
2018-12-15 22:40:07 +01:00
}
\details{
Ages below 0 will be returned as \code{NA} with a warning. Ages above 120 will only give a warning.
2021-05-23 23:11:16 +02:00
This function vectorises over both \code{x} and \code{reference}, meaning that either can have a length of 1 while the other argument has a larger length.
}
2019-01-25 13:18:41 +01:00
\examples{
2022-08-21 16:37:20 +02:00
# 10 random pre-Y2K birth dates
df <- data.frame(birth_date = as.Date("2000-01-01") - runif(10) * 25000)
# add ages
2019-01-25 13:18:41 +01:00
df$age <- age(df$birth_date)
2022-08-21 16:37:20 +02:00
# add exact ages
df$age_exact <- age(df$birth_date, exact = TRUE)
2022-08-21 16:37:20 +02:00
# add age at millenium switch
df$age_at_y2k <- age(df$birth_date, "2000-01-01")
df
2019-01-25 13:18:41 +01:00
}
2018-12-15 22:40:07 +01:00
\seealso{
To split ages into groups, use the \code{\link[=age_groups]{age_groups()}} function.
2018-12-15 22:40:07 +01:00
}