get.yrs: Convert date objects to fractional years

View source: R/fractional_years.R

get.yrsR Documentation

Convert date objects to fractional years

Description

Using Date objects, calculates given dates as fractional years.

Usage

get.yrs(x, year.length = "approx", ...)

Arguments

x

a Date object, or anything that link{as.Date} accepts

year.length

character string, either 'actual' or 'approx'; can be abbreviated; see Details

...

additional arguments passed on to as.Date; typically format when x is a character string variable, and origin when x is numeric

Details

x should preferably be a date, Date or IDate object, although it can also be a character string variable which is coerced internally to Date format using as.Date.character.

When year.length = 'actual' , fractional years are calculated as year + (day_in_year-1)/365 for non-leap-years and as year + (day_in_year-1)/366 for leap years. If year.length = 'approx' , fractional years are always calculated as in year + (day_in_year-1)/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 .

Value

A numeric vector of fractional years.

Author(s)

Joonas Miettinen

See Also

cal.yr, as.Date.yrs, as.Date

Examples


data("sire")
sire$dg_yrs <- get.yrs(sire$dg_date)
summary(sire$dg_yrs)

## see: ?as.Date.yrs
dg_date2 <- as.Date(sire$dg_yrs)
summary(as.numeric(dg_date2 - as.Date(sire$dg_date)))

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

## "..." passed on to as.Date, so character / numeric also accepted as input
## (and whatever else as.Date accepts)
get.yrs("2000-06-01")
get.yrs("20000601", format = "%Y%m%d")
get.yrs("1/6/00", format = "%d/%m/%y")

get.yrs(100, origin = "1970-01-01")



WetRobot/popEpi documentation built on Aug. 29, 2023, 3:53 a.m.