R/calculate_age.R

Defines functions calculate_age

Documented in calculate_age

#' Determine people's age
#'
#' Finds out the calendar year difference between two dates.
#'
#' @param givendate,birthdate Date vectors of equal length.
#'
#' @return An integer vector of the same length as \code{givendate}.
#' @details For \code{birthdate > givendate} \code{NA}s are returned .
#' @export
#' @import lubridate
calculate_age <- function(givendate, birthdate) {
  stopifnot(is.Date(c(givendate, birthdate)))

  output <- as.period(interval(birthdate, givendate))$year
  output[output < 0] <- NA
  output
}
GuillermoNasarre/ihr documentation built on April 9, 2021, 9:44 a.m.