Learn R Programming

grates (version 1.4.3)

month_class: Month class

Description

Month objects are groupings of 'n consecutive months' stored relative to the Unix Epoch. More precisely, grates_month objects are stored as the integer number (starting at 0), of n-month groups since the Unix Epoch (1970-01-01).

Usage

as_month(x, n, ...)

# S3 method for default as_month(x, n, ...)

# S3 method for Date as_month(x, n, ...)

# S3 method for POSIXt as_month(x, n, ...)

# S3 method for character as_month(x, n, ...)

# S3 method for factor as_month(x, n, ...)

new_month(x = integer(), n)

is_month(xx)

Value

A <grates_month> object.

Arguments

x, xx

R objects.

n

[integer]

Number of months that are being grouped. Must be greater than 1 (use as_yearmonth() for this case).

...

Only used For character input where additional arguments are passed through to as.Date().

Details

as_month() is a generic for conversion to <grates_month>.

  • Character input is first parsed using as.Date().

  • POSIXt inputs are converted with the timezone respected.

  • Precision is only to the month level (i.e. the day of the month is always dropped).

new_month() is a minimal constructor for <grates_month> objects aimed at developers. It takes, as input x, the number of n-months since the Unix Epoch (1970-01-01) and the related value of n. double vectors will be converted via as.integer(floor(x)).

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

The yearmonth class.

Examples

Run this code

# date coercion
as_month(Sys.Date(), n = 2)

# character coercion
as_month("2019-05-03", n = 4)

# POSIXt coercion
as_month(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"), n = 2)

# direct construction
d <- seq.Date(from = as.Date("1970-03-01"), by = "2 month", length.out = 10)
stopifnot(
    identical(
        as_month(d, n = 2),
        new_month(1:10, 2)
    )
)

Run the code above in your browser using DataLab