as_period: Coerce an object to period

View source: R/period.R

as_periodR Documentation

Coerce an object to period

Description

as_period() is a generic for coercing input in to ⁠<grates_period>⁠.

Usage

as_period(x, n, ...)

## Default S3 method:
as_period(x, n = 1L, offset = 0L, ...)

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

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

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

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

Arguments

x

An R object:

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

  • POSIXt inputs are converted with the timezone respected.

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 ⁠[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.

Value

A ⁠<grates_period>⁠ object.

Note

Internally grates_period objects are stored as the integer number, starting at 0L, of periods since the Unix Epoch (1970-01-01) and a specified offset. Here periods are taken to mean groupings of n consecutive days.

See Also

as.Date()

Examples

as_period("2019-05-03")
as_period("2019-05-03", n = 2, offset = 1)
as_period(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"), n = 10)
as_period(as.Date("2020-03-02"), n = 2L, offset = as.Date("2020-03-01"))


grates documentation built on July 9, 2023, 7:09 p.m.