mondate-methods {mondate} | R Documentation |
All purpose mondate constructor / coercer.
mondate(x, displayFormat=.default.displayFormat, timeunits=.default.timeunits, ...) ## S4 method for signature 'mondate' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'numeric' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'Date' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'POSIXt' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'character' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'array' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'missing' mondate(x, displayFormat, timeunits, ...) ## S4 method for signature 'ANY' mondate(x, displayFormat, timeunits, ...)
x |
an R object to convert to a |
displayFormat |
character string representing the date format with which to display
the |
timeunits |
character string "months" (default), "years", or "days" indicating the units in which date arithmetic will be carried out. |
... |
arguments to be passed to other methods. |
signature(x = "mondate")
For mondate
x, this could be a way to copy a mondate
and perhaps change
the mondate
's displayFormat
or timeunits
slots
in the process.
For any class that extends mondate
,
use of this method will return the underlying mondate class without
additional slots (if any) of the subclass.
signature(x = "numeric")
For numeric
x, the simplest case is when timeunits
= "months",
in which case
the value of x and properties displayFormat
and timeunits
are simply stored.
If timeunits
= "years" then it
is presumed that the value of x represents the number of years since the
beginning of the millennium,
in which case the value of x is multiplied by 12
and then stored.
If timeunits
= "days" then it is presumed
that the value of x represents the number of days since the beginning
of the millennium,
in which case the value is calculated using as.Date
.
Note that infinite values of x
are allowed
("infinite" dates are helpful in modeling events "at ultimate"
in actuarial, longitudinal, and time series analyses).
signature(x = "Date")
signature(x = "POSIXt")
For a date x, as.POSIXlt
is used to convert to an ISO standard date,
from which the number of months of that day since the beginning of the
millennium is calculated.
signature(x = "character")
If displayFormat
is provided, then that format
is used to attempt to convert the character value to a date.
Otherwose,
characters are converted to dates using the first format found in the
set of valid formats.
The current set is the vector
c("%m/%d/%Y", "%m-%d-%Y", "%Y-%m-%d", "%Y/%m/%d").
If none of the valid formats successfully converts x
to a date,
then as a last resort
the character string is attempted to be coerced to a
numeric
and then
to a mondate
.
signature(x = "array")
If an object x
is an array
,
then this method enables the mondate
to inherit its shape.
After that, other "signatures" take over.
signature(x = "missing")
Enables the call mondate()
to work.
Useful for prototypes, e.g.
Body of method is simply new("mondate")
.
signature(x = "ANY")
For any other class of x an attempt will be made to convert
to Date
("as.Date(x)
").
If unsuccessful, an attempt will be made to convert to numeric
;
if successful, a warning will be issued to check the results
relative to the numeric
conversion, otherwise execution will
be stop
ped.
mondate("1-31-2010") # Jan. 31, 2010 mondate(60) # 60 months after 12/31/1999, so Dec. 31, 2004 dat <- as.Date("2010-1-31") (M <- mondate(dat)) # Jan. 31, 2010 x <- 12 * 1:6 mondate(x) # first 6 yearends in 2000's y <- x + 12 mondate(cbind(x,y)) # bounding dates of first 6 years of millennium (y <- mondate(1:6,timeunits="years")) # first 6 yearends, 'years' timeunits # The results of date arithmetic on y will be displayed in "years". # E.g., the differences of y can be calculated as: tail(y,-1) - head(y,-1)# vector of five 1's, with "timeunits" attribute = "years" as.numeric(x) as.numeric(y) # the underlying numeric representations are the same # Demonstrating "infinite" dates y <- c(y,Inf) y # last element shows as Inf tail(y,-1) - head(y,-1)# last element is now infinity # The zoo examples point out a difference between zoo and mondate. # zoo assumes that the zero-th part of a month or quarter is the first # day of the month or quarter, whereas mondate assumes that it is # the instant before the first day of the month or quarter. # Since frac=0 is zoo's as.Date coersion default, a month or quarter in # zoo's sense converts to the end of the first day rather than # the beginning. library(zoo) x <- ts(1:10, frequency = 4, start = c(1959, 2)) # starting 2nd qtr of 1959 x # There is no method for class 'ts' so x is coerced (successfully) # because that class has an as.Date method, but with a warning. # The result is a vector of length 10 representing the close of business # at the end of the first day of each of the given quarters. mondate(x) # The yearmon class will identify any day in June 2010 with that month. as.yearmon("2010-6-15") mondate(as.yearmon("2010-6-15")) # end of first day of June 2010 mondate(as.yearmon("2010-6-15"), frac=1) # end of last day of June 2010 mondate(as.yearqtr("2010-2"), frac=1) # same # The if missing, displayFormat will be determined from the character input x <- mondate("2010-12-31") x # x displays in the input European format # The provided, displayFormat must match the format of the character input # or NA's will result. mondate("2010-12-31", displayFormat = "%m-%d-%Y") # results in NA # Always display x using just the year x <- mondate(as.Date("2012-3-1"), displayFormat="%Y") x # shows as the year 2012, but month and day are nevertheless retained month(x) # 3 day(x) # 1