cal.yr {Epi}R Documentation

Functions to convert character, factor and various date objects into a number, and vice versa.

Description

Dates are converted to a numerical value, giving the calendar year as a fractional number. 1 January 1970 is converted to 1970.0, and other dates are converted by assuming that years are all 365.25 days long, so inaccuracies may arise, for example, 1 Jan 2000 is converted to 1999.999. Differences between converted values will be 1/365.25 of the difference between corresponding Date objects.

Usage

  cal.yr(x, format)
  as.Date.cal.yr( obj )
  as.Date.numeric( obj, unit="d" )

Arguments

x A factor or character vector, representing a date in format format, or an object of class Date, POSIXlt, POSIXct, date, dates or chron (the latter two requires the chron package).
format Format of the date values if x is factor or character
unit Which units are the date measured in, "y" for years, "d" for days.

Value

cal.yr returns a numerical vector of the same length as x, of class c("cal.yr","numeric").
as.Date.cal.yr and as.Date.numeric return Date objects.

Author(s)

Bendix Carstensen, Steno Diabetes Center & Dept. of Biostatistics, University of Copenhagen, bxc@steno.dk, http://www.pubhealth.ku.dk/~bxc

See Also

DateTimeClasses, Date

Examples

 birth <- c("14/07/1852","01/04/1954","10/06/1987","16/05/1990",
            "01/01/1996","01/01/1997","01/01/1998","01/01/1999")
 birth.dat <- as.Date( birth, format="
 bt.yr <- cal.yr( birth, format="%d/%m/%Y" )
 bt.dat <- as.Date( bt.yr )
 # To get readable output:
 data.frame( birth, birth.dat, bt.yr, (bt.yr-1970)*365.25, bt.dat )

[Package Epi version 0.5.9 Index]