Learn R Programming

popEpi (version 0.2.1)

get.yrs: Convert date objects to fractional years

Description

Using Date objects, calculates given dates as fractional years.

Usage

get.yrs(dates, format = "%Y-%m-%d", year.length = "approx")

Arguments

dates
a vector or column of Date objects or right kind of character strings, see Details
format
a character string; if dates is a character vector, specifies the format; see as.Date
year.length
character string, either 'actual' or 'approx'; can be abbreviated; see Details

Details

dates should preferably be a date, Date or IDate object, although they can also be character strings in a format specified by format (passed to as.Date). When year.length = 'actual', fractional years are calculated as year + day_in_year/365 for non-leap-years and as year + day_in_year/366 for leap years. If year.length = 'approx', fractional years are always calculated as in year + day_in_year/365.242199. There is a slight difference, then, between the two methods when calculating durations between fractional years. For meticulous accuracy one might instead want to calculate durations using dates (days) and convert the results to fractional years. Note that dates are effectively converted to fractional years at 00:00:01 o'clock: get.yrs("2000-01-01") = 2000, and get.yrs("2000-01-02") = 2000 + 1/365.242199.

See Also

cal.yr

Examples

Run this code
test <- copy(sire)
test$dg_yrs <- get.yrs(test$dg_date)
summary(test$dg_yrs)

## Epi's cal.yr versus get.yrs
Epi::cal.yr("2000-01-01") ## 1999.999
get.yrs("2000-01-01") ## 2000

Run the code above in your browser using DataLab