Skip to contents

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

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, ...)

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

Examples

as_period("2019-05-03")
#> <grates_period[1]>
#> [1] "2019-05-03"
as_period("2019-05-03", n = 2, offset = 1)
#> <grates_period[1]>
#> [1] "2019-05-03 to 2019-05-04"
as_period(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"), n = 10)
#> <grates_period[1]>
#> [1] "2019-02-23 to 2019-03-04"
as_period(as.Date("2020-03-02"), n = 2L, offset = as.Date("2020-03-01"))
#> <grates_period[1]>
#> [1] "2020-03-01 to 2020-03-02"