Arith-methods | R Documentation |
Arithmetic methods for class mondate
.
Includes three special "units between" methods.
signature(e1 = "mondate", e2 = "mondate")
Performs arithmetic operations on two mondate
s.
The result will be numeric
with attribute timeunits
belonging to the slot of the first argument
(with a warning if the two arguments have different timeunits
).
The only operation that would seem useful for two
mondate
s is subtraction.
signature(e1 = "mondate", e2 = "numeric")
signature(e1 = "numeric", e2 = "mondate")
signature(e1 = "mondate", e2 = "array")
signature(e1 = "array", e2 = "mondate")
Performs arithmetic operations of a numeric
on a
mondate
where the units
of the numeric
is taken from the
timeunits
slot of the mondate
.
The result will be a mondate
with the same properties
as the mondate
in the function call.
The only operations that would seem useful for a mondate
and a numeric
are addition and subtraction.
Most of the time it is expected that timeunits
="months" so that,
for example,
adding/subtracting a number to/from that mondate
adds/subtracts that
number of months.
If the mondate
's timeunits
="years"
then the perceived intention is to
add/subtract that number of years.
To accomplish that, the mondate
's numeric
value is divided by 12, the operation is performed, and the result converted
to a mondate
.
If the mondate
's timeunits
="days",
then the mondate
's value is converted to the number
of days since the start of the millennium,
the operation is performed
(e.g., the numeric
number of days is added or subtracted),
and the result converted to a mondate
.
(See the convert
option of the as.numeric("mondate")
function.)
+signature(e1 = "mondate", e2 = "difftime")
-signature(e1 = "mondate", e2 = "difftime")
Use a difftime object to add and subtract
secs, days, weeks, months, and years
to or from a mondate
.
-signature(e1 = "mondate", e2 = "mondate")
Returns a difftime object equal to the signed number of units between
e1
and e2
,
where "units" = timeunits(e1)
, with a warning
if e1
and e2
have differing timeunits
.
MonthsBetween(from = "mondate", to = "mondate")
Same as abs(from - to)
in months.
YearsBetween(from = "mondate", to = "mondate")
Same as abs(from - to)
in years,
which is also the number of months between divided by 12.
DaysBetween(from = "mondate", to = "mondate")
Same as abs(from - to)
in days,
which is also the difference between the as.Date
representation of from
and to
.
Arith
M <- mondate("1-1-2010") # will display in U.S. date format
M - 1:12 # the 1st of the month for all months in 2009
# in reverse chronological order; inherits M's displayFormat
x <- mondate(matrix(12 * 1:4, 2, 2)) # 2x2 matrix of 2000-2003 year ends
x
y <- x + 12 # one year later, also a matrix
y
y - x # 2x2 matrix of 12s, with an attribute ("months")
MonthsBetween(x, y) # same, without the attribute
YearsBetween(x, y)
DaysBetween(x, y)
## Use difftime object to add, subtract secs, days, weeks, months, years
x <- mondate(1) # January 31, 2000
y <- as.difftime(1, units = "days")
x + y
x - y
yw <- as.difftime(1, units = "weeks")
x + yw
x - yw
x + as.difftime(1, , "days")
x + as.difftime(86400, , "secs")
x <- mondate.ymd(2012, 2, 29) # leap day
x + as.difftime(1, , "years") # last day of February 2013, not a leap day
x - as.difftime(1, , "months") # not the last day of January
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.