#' Age calucaltion function
#'
#' param dob date of birth
#' param enddate date to calculate difference from
#' param units "days", "months", "years"
#'
age_calc <- function(dob, enddate = Sys.Date(), units = 'months'){
if (!inherits(dob, "Date") | !inherits(enddate, "Date"))
stop("Both dob and enddate must be Date class objects")
start <- as.POSIXlt(dob)
end <- as.POSIXlt(enddate)
years <- end$year - start$year
if(units == 'years'){
result <- ifelse((end$mon < start$mon) |
((end$mon == start$mon) & (end$mday < start$mday)),
years - 1, years)
} else if(units == 'months'){
months <- (years-1) * 12
result <- months + start$mon
} else if(units == 'days'){
result <- difftime(end, start, units = 'days')
} else{
stop("Unrecognized units. Please choose years, months, or days.")
}
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.