yearmonth_class {grates} | R Documentation |
Yearmonth class
Description
<grates_yearmonth>
objects represent, unsurprisingly, years and associated months.
Internally they are stored as the number of months (starting at 0) since the
Unix Epoch (1970-01-01). Precision is only to the month level (i.e. the day
of the month is always dropped).
Usage
yearmonth(year = integer(), month = integer())
as_yearmonth(x, ...)
## Default S3 method:
as_yearmonth(x, ...)
## S3 method for class 'Date'
as_yearmonth(x, ...)
## S3 method for class 'POSIXt'
as_yearmonth(x, ...)
## S3 method for class 'character'
as_yearmonth(x, ...)
## S3 method for class 'factor'
as_yearmonth(x, ...)
new_yearmonth(x = integer())
is_yearmonth(xx)
Arguments
year |
Vector representing the year associated with
|
month |
Vector representing the month associated with
|
x , xx |
R objects. |
... |
Only used for character input where additional arguments are passed through
to |
Details
yearmonth()
is a constructor for <grates_yearmonth>
objects. It takes a
vector of year and a vector of month values as inputs. Length 1 inputs will
be recycled to the length of the other input and double
vectors will
be converted to integer via as.integer(floor(x))
.
as_yearmonth()
is a generic for coercing input in to <grates_yearmonth>
.
Character input is first parsed using
as.Date()
.POSIXct and POSIXlt are converted with their timezone respected.
new_yearmonth()
is a minimal constructor for <grates_yearmonth>
objects
aimed at developers. It takes, as input, the number of months (starting at 0)
since the Unix Epoch, that you wish to represent. double
vectors will again
be converted to integer via as.integer(floor(x))
.
Value
A <grates_yearmonth>
object.
References
The algorithm to convert between dates and months relative to the UNIX Epoch comes from the work of Davis Vaughan in the unreleased datea package
See Also
new_month()
and as_month()
and for grouping of consecutive months.
Examples
# date coercion
as_yearmonth(Sys.Date())
# POSIXt coercion
as_yearmonth(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"))
# character coercion
as_yearmonth("2019-05-03")
# construction
yearmonth(year = 2000, month = 3)
# direct construction
d <- seq.Date(from = as.Date("1970-01-01"), by = "month", length.out = 10)
stopifnot(
identical(
as_yearmonth(d),
new_yearmonth(0:9)
)
)