Learn R Programming

grates (version 1.4.3)

period_class: Period class

Description

<grates_period> objects represent groupings of n consecutive days calculated relative to an offset. It is useful for when you wish to group an arbitrary number of dates together (e.g. 10 days).

Usage

as_period(x, n, ...)

# S3 method for default as_period(x, n = 1L, offset = 0L, ...)

# S3 method for Date as_period(x, n = 1L, offset = 0L, ...)

# S3 method for POSIXt as_period(x, n = 1L, offset = 0L, ...)

# S3 method for character as_period(x, n = 1L, offset = 0L, ...)

# S3 method for factor as_period(x, n = 1L, offset = 0L, ...)

new_period(x = integer(), n = 1L, offset = 0L)

is_period(xx)

Value

A <grates_period> object.

Arguments

x, xx

R objects.

For as_period() this is the object to be coerced.

For new_period() this represents the number of periods since the Unix Epoch (1970-01-01) and a specified offset.

n

[integer]

Number of days that are being grouped.

...

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

offset

[integer] or, for as_period(), a [date].

Value you wish to start counting periods from relative to the Unix Epoch:

  • For integer values this is stored scaled by n (offset <- as.integer(offset) %% n).

  • For date values this is first converted to an integer offset (offset <- floor(as.numeric(offset))) and then scaled via n as above.

Details

Internally grates_period objects are stored as the integer number, starting at 0, of periods since the Unix Epoch (1970-01-01) and a specified offset. Here periods are taken to mean groupings of n consecutive days. For storage and calculation purposes, offset is scaled relative to n (i.e. offset <- offset %% n) and values of x stored relative to this scaled offset.

as_period() is a generic for coercing input in to <grates_period> objects. It is the recommended way for constructing period objects as it allows the offset to be specified as a date (rather than an integer value relative to the Unix Epoch).

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

  • POSIXct and POSIXlt are converted with their timezone respected.

new_period() is a minimal constructor for <grates_period> objects aimed at developers. It takes, as input, the number of periods since the Unix Epoch and the specified offset. double vectors will be converted via as.integer(floor(x)).

Examples

Run this code

# coercion from date
dat <- as.Date("2012-12-01")
as_period(dat + 0:3, n = 2, offset = dat)

# direct construction
new_period(1:10)

Run the code above in your browser using DataLab