R: Functions to convert character, factor and various date...
cal.yr
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="%Y-%m-%d", wh=NULL )
## S3 method for class 'cal.yr'
as.Date( x, ... )
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).
If x is a data frame, all variables in the data-frame
which are of one the classes mentioned are converted to class cal.yr.
See arguemt wh, though.
format
Format of the date values if x is factor or character.
If this argument is supplied and x is a datafame, all
character variables are converted to class cal.yr.
Factors in the dataframe will be ignored.
wh
Indices of the variables to convert if x is a data frame.
Can be either a numerical or character vector.
...
Arguments passed on from other methods.
Value
cal.yr returns a numerical vector of the same length as
x, of class c("cal.yr","numeric"). If x is a data frame
a dataframe with some of the columns converted to class "cal.yr" is
returned.
# Character vector of dates:
birth <- c("14/07/1852","01/04/1954","10/06/1987","16/05/1990",
"12/11/1980","01/01/1997","01/01/1998","01/01/1999")
# Proper conversion to class "Date":
birth.dat <- as.Date( birth, format="%d/%m/%Y" )
# Converson of character to class "cal.yr"
bt.yr <- cal.yr( birth, format="%d/%m/%Y" )
# Back to class "Date":
bt.dat <- as.Date( bt.yr )
# Numerical calculation of days since 1.1.1970:
days <- Days <- (bt.yr-1970)*365.25
# Blunt assignment of class:
class( Days ) <- "Date"
# Then data.frame() to get readable output of results:
data.frame( birth, birth.dat, bt.yr, bt.dat, days, Days, round(Days) )
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Epi)
Attaching package: 'Epi'
The following object is masked from 'package:base':
merge.data.frame
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Epi/cal.yr.Rd_%03d_medium.png", width=480, height=480)
> ### Name: cal.yr
> ### Title: Functions to convert character, factor and various date objects
> ### into a number, and vice versa.
> ### Aliases: cal.yr as.Date.cal.yr
> ### Keywords: manip chron
>
> ### ** Examples
>
> # Character vector of dates:
> birth <- c("14/07/1852","01/04/1954","10/06/1987","16/05/1990",
+ "12/11/1980","01/01/1997","01/01/1998","01/01/1999")
> # Proper conversion to class "Date":
> birth.dat <- as.Date( birth, format="%d/%m/%Y" )
> # Converson of character to class "cal.yr"
> bt.yr <- cal.yr( birth, format="%d/%m/%Y" )
> # Back to class "Date":
> bt.dat <- as.Date( bt.yr )
> # Numerical calculation of days since 1.1.1970:
> days <- Days <- (bt.yr-1970)*365.25
> # Blunt assignment of class:
> class( Days ) <- "Date"
> # Then data.frame() to get readable output of results:
> data.frame( birth, birth.dat, bt.yr, bt.dat, days, Days, round(Days) )
birth birth.dat bt.yr bt.dat days Days round.Days.
1 14/07/1852 1852-07-14 1852.535 1852-07-14 -42904 1852-07-14 1852-07-14
2 01/04/1954 1954-04-01 1954.246 1954-04-01 -5754 1954-03-31 1954-04-01
3 10/06/1987 1987-06-10 1987.437 1987-06-10 6369 1987-06-10 1987-06-10
4 16/05/1990 1990-05-16 1990.370 1990-05-16 7440 1990-05-15 1990-05-16
5 12/11/1980 1980-11-12 1980.864 1980-11-12 3968 1980-11-11 1980-11-12
6 01/01/1997 1997-01-01 1997.001 1997-01-01 9862 1997-01-01 1997-01-01
7 01/01/1998 1998-01-01 1998.000 1998-01-01 10227 1998-01-01 1998-01-01
8 01/01/1999 1999-01-01 1998.999 1999-01-01 10592 1998-12-31 1999-01-01
>
>
>
>
>
> dev.off()
null device
1
>