Title: | Functions for Handling Dates |
---|---|
Description: | Functions for handling dates. |
Authors: | Terry Therneau [aut] (S original), Thomas Lumley [trl] (R port), Kjetil Halvorsen [trl] (R port), Kurt Hornik [trl, aut, cre] (<https://orcid.org/0000-0003-4198-9911>, R port), R Core Team [ctb] |
Maintainer: | Kurt Hornik <[email protected]> |
License: | GPL-2 |
Version: | 1.2-42 |
Built: | 2024-11-06 03:25:21 UTC |
Source: | https://github.com/cran/date |
Converts any of the following character forms to a Julian date: 8/31/56, 8-31-1956, 31 8 56, 083156, 31Aug56, or August 31 1956.
as.date(x, order = "mdy", ...)
as.date(x, order = "mdy", ...)
x |
input data vector. |
order |
if |
... |
if |
If x
is numeric, then floor(x)
is returned, e.g.,
as.date(35)
is the same as as.date(35.2)
and gives
February 5, 1960 (‘5Feb60’). If x
is character, the
program attempts to parse it.
For each date, the number of days between it and January 1, 1960. The date will be missing if the string is not interpretable.
mdy.date
,
date.mmddyy
,
date.ddmmmyy
as.date(c("1jan1960", "2jan1960", "31mar1960", "30jul1960"))
as.date(c("1jan1960", "2jan1960", "31mar1960", "30jul1960"))
Given a vector of Julian dates, this returns them in the form “10Nov89”, “28Jul54”, etc.
date.ddmmmyy(sdate)
date.ddmmmyy(sdate)
sdate |
A vector of Julian dates, e.g., as returned by
|
A vector of character strings containing the formatted dates.
date.ddmmmyy(1:10)
date.ddmmmyy(1:10)
Convert a vector of Julian dates to a list of vectors with the corresponding values of month, day and year, and optionally weekday.
date.mdy(sdate, weekday = FALSE)
date.mdy(sdate, weekday = FALSE)
sdate |
a Julian date value, as returned by |
weekday |
if |
A list with components month
, day
, and year
.
Press, W. H., Teukolsky, S. A., Vetterling, W. T., and Flannery, B. P. (1992). Numerical Recipes: The Art of Scientific Computing (Second Edition). Cambridge University Press.
day <- 7 temp <- date.mdy(mdy.date(month = 7, day = day, year = 1960)) ## Check for illegal dates, such as 29 Feb in a non leap year if (temp$day != day) { cat("Some illegal dates\n") } else { cat("All days are legal\n") }
day <- 7 temp <- date.mdy(mdy.date(month = 7, day = day, year = 1960)) ## Check for illegal dates, such as 29 Feb in a non leap year if (temp$day != day) { cat("Some illegal dates\n") } else { cat("All days are legal\n") }
Given a vector of Julian dates, this returns them in the form “10/11/89”, “28/7/54”, etc.
date.mmddyy(sdate, sep = "/")
date.mmddyy(sdate, sep = "/")
sdate |
A vector of Julian dates, e.g., as returned by
|
sep |
Character string used to separate the month, day, and year portions of the returned string. |
A vector of character strings containing the formatted dates.
date.mdy
,
mdy.date
,
date.ddmmmyy
date.mmddyy(as.date(10))
date.mmddyy(as.date(10))
Given a vector of Julian dates, this returns them in the form “10/11/1989”, “28/7/1854”, etc.
date.mmddyyyy(sdate, sep = "/")
date.mmddyyyy(sdate, sep = "/")
sdate |
A vector of Julian dates, e.g., as returned by
|
sep |
Character string used to separate the month, day, and year portions of the returned string. |
A vector of character strings containing the formatted dates.
date.mdy
,
mdy.date
,
date.ddmmmyy
date.mmddyyyy(as.date(1:10))
date.mmddyyyy(as.date(1:10))
Objects of class "date"
.
is.date(x)
is.date(x)
x |
any R object. |
Dates are stored as the number of days since 1/1/1960, and are kept
in integer format. (This is the same baseline value as is used by
SAS). The numerical methods for dates treat date - date
as a
numeric, and date +- numeric
as a date.
is.date
returns TRUE
if x
has class
"date"
, and FALSE
otherwise. Its behavior is unaffected
by any attributes of x
; for example, x
could be a date
array (in contrast to the behavior of is.vector
).
as.date
returns x
if x
is a simple object of
class "date"
, and otherwise a date vector of the same length as
x
and with data resulting from coercing the elements of
x
to class "date"
. See the manual page for
as.date()
for details.
Logical operations as well as the numeric functions exp()
,
log()
, and so on are invalid.
Other methods exist for missing value, as.character()
,
printing, and summarizing.
date.mdy
,
mdy.date
,
date.ddmmmyy
,
as.date
.
Given a month, day, and year, returns the number of days since January 1, 1960.
mdy.date(month, day, year, nineteen = TRUE, fillday = FALSE, fillmonth = FALSE)
mdy.date(month, day, year, nineteen = TRUE, fillday = FALSE, fillmonth = FALSE)
month |
vector of months. |
day |
vector of days. |
year |
vector of years. |
nineteen |
if |
fillday |
if |
fillmonth |
if |
The date functions are particularly useful in computing time spans, such as number of days on test, and similar functions can be found in other statistical packages. The baseline date of Jan 1, 1960 is, of course, completely arbitrary (it is the same one used by SAS).
The fillday
and fillmonth
options are perhaps useful
only to the author and a very few others: we sometimes deal with
patients whose birth date was in the 1800's, and only the month or
even only the year is known. When the interval is greater than 80
years, a filler seems defensible.
a vector of Julian dates.
Press, W. H., Teukolsky, S. A., Vetterling, W. T., and Flannery, B. P. (1992). Numerical Recipes: The Art of Scientific Computing (Second Edition). Cambridge University Press.
date.mmddyy
,
date.ddmmmyy
,
date.mmddyyyy
mdy.date(3, 10, 53) xzt <-1:10 xzy <- as.date(xzt) test <- data.frame(x = xzt, date = xzy) summary(test)
mdy.date(3, 10, 53) xzt <-1:10 xzy <- as.date(xzt) test <- data.frame(x = xzt, date = xzy) summary(test)