Learn R Programming

grates (version 1.4.3)

yearquarter_class: Yearquarter class

Description

<grates_yearquarter> objects represent years and associated quarters Internally they are stored as the number of quarters (starting at 0) since the Unix Epoch (1970-01-01).

Usage

yearquarter(year = integer(), quarter = integer())

as_yearquarter(x, ...)

# S3 method for default as_yearquarter(x, ...)

# S3 method for Date as_yearquarter(x, ...)

# S3 method for POSIXt as_yearquarter(x, ...)

# S3 method for character as_yearquarter(x, ...)

# S3 method for factor as_yearquarter(x, ...)

new_yearquarter(x = integer())

is_yearquarter(xx)

Value

A <grates_yearquarter> object.

Arguments

year

[integer]

Vector representing the year associated with quarter.

double vectors will be converted via as.integer(floor(x)).

quarter

[integer]

Vector representing the quarter associated with year.

double vectors will be converted via as.integer(floor(x)).

x, xx

R objects.

...

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

Details

yearquarter() is a constructor for <grates_yearquarter> objects. It takes a vector of year and a vector of quarter 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_yearquarter() is a generic for coercing input in to <grates_yearquarter>.

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

  • POSIXct and POSIXlt are converted with their timezone respected.

new_yearquarter() is a minimal constructor for <grates_yearquarter> objects aimed at developers. It takes, as input, the number of quarters (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)).

Examples

Run this code

# date coercion
as_yearquarter(Sys.Date())

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

# character coercion
as_yearquarter("2019-05-03")

# construction
yearquarter(year = 2000, quarter = 3)

# direct construction
d <- seq.Date(from = as.Date("1970-01-01"), by = "quarter", length.out = 4)
stopifnot(
    identical(
        as_yearquarter(d),
        new_yearquarter(0:3)
    )
)


Run the code above in your browser using DataLab