Arith-methods {mondate} | R Documentation |
Methods for Function Group Arith on mondate Objects
Description
Arithmetic methods for class mondate
.
Includes three special "units between" methods.
Methods
signature(e1 = "mondate", e2 = "mondate")
-
Performs arithmetic operations on two
mondate
s. The result will benumeric
with attributetimeunits
belonging to the slot of the first argument (with a warning if the two arguments have differenttimeunits
). The only operation that would seem useful for twomondate
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 amondate
where the units of thenumeric
is taken from thetimeunits
slot of themondate
. The result will be amondate
with the same properties as themondate
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 thatmondate
adds/subtracts that number of months. If themondate
'stimeunits
="years" then the perceived intention is to add/subtract that number of years. To accomplish that, themondate
's numeric value is divided by 12, the operation is performed, and the result converted to amondate
. If themondate
'stimeunits
="days", then themondate
's value is converted to the number of days since the start of the millennium, the operation is performed (e.g., thenumeric
number of days is added or subtracted), and the result converted to amondate
. (See theconvert
option of theas.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
ande2
, where "units" =timeunits(e1)
, with a warning ife1
ande2
have differingtimeunits
. 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 theas.Date
representation offrom
andto
.
See Also
Examples
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