yearweek_class: Yearweek class

yearweek_classR Documentation

Yearweek class

Description

Yearweeks start on a user defined day of the week and span a 7 day period. For yearweek objects the first week of a "year" is considered to be the first yearweek containing 4 days of the given calendar year. This means that the calendar year will sometimes be different to that of the associated yearweek object.

Internally, ⁠<grates_yearweek>⁠ objects are stored as the number of weeks (starting at 0) from the date of the user-specified firstday nearest the Unix Epoch (1970-01-01). That is, the number of seven day periods from:

- 1969-12-29 for `firstday` equal to 1 (Monday)
- 1969-12-30 for `firstday` equal to 2 (Tuesday)
- 1969-12-31 for `firstday` equal to 3 (Wednesday)
- 1970-01-01 for `firstday` equal to 4 (Thursday)
- 1970-01-02 for `firstday` equal to 5 (Friday)
- 1970-01-03 for `firstday` equal to 6 (Saturday)
- 1970-01-04 for `firstday` equal to 7 (Sunday)

Usage

yearweek(year = integer(), week = integer(), firstday = 1L)

as_yearweek(x, ...)

## Default S3 method:
as_yearweek(x, ...)

## S3 method for class 'Date'
as_yearweek(x, firstday = 1L, ...)

## S3 method for class 'POSIXt'
as_yearweek(x, firstday = 1L, ...)

## S3 method for class 'character'
as_yearweek(
  x,
  firstday = 1L,
  format,
  tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
  ...
)

## S3 method for class 'factor'
as_yearweek(
  x,
  firstday = 1L,
  format,
  tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
  ...
)

new_yearweek(x = integer(), firstday = 1L)

is_yearweek(xx)

Arguments

year

⁠[integer]⁠

Vector representing the year associated with week.

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

week

⁠[integer]⁠

Vector representing the week associated with 'year.

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

firstday

⁠[integer]⁠

The day the week starts on from 1 (Monday) to 7 (Sunday).

x, xx

R objects.

...

Other values passed to as.Date().

format

⁠[character]⁠

Passed to as.Date() unless format = "yearweek" in which case input is assumed to be in the form "YYYY-Wxx".

If not specified, it will try tryFormats one by one on the first non-NA element, and give an error if none works. Otherwise, the processing is via strptime() whose help page describes available conversion specifications.

tryFormats

⁠[character]⁠

Format strings to try if format is not specified.

Details

yearweek() is a constructor for ⁠<grates_yearweek>⁠ objects. These are weeks whose first day can be specified by the user. It takes a vector of year and vector of week values as inputs. Length 1 inputs will be recycled to the length of the other input and double vectors will again be converted to integer via as.integer(floor(x)).

as_yearweek() is a generic for conversion to ⁠<grates_yearweek>⁠.

  • Date, POSIXct, and POSIXlt are converted with the timezone respected.

  • Character objects are first coerced to date via as.Date() unless format = "yearweek" in which case input is assumed to be in the form "YYYY-Wxx" and parsed accordingly.

new_yearweek() is a minimal constructor for ⁠<grates_yearweek>⁠ objects aimed at developers. It takes, as input, the number of weeks since the user-specified firstday nearest the Unix Epoch. double vectors will be converted to integer via as.integer(floor(x)).

Value

A ⁠<grates_yearweek>⁠ object with subclass corresponding to the first day of the week they represent (e.g. ⁠<grates_yearweek_monday>⁠).

See Also

new_isoweek() and new_epiweek().

Examples


# date coercion
as_yearweek(Sys.Date())

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

# character coercion with Friday as the first day of the week
as_yearweek("2019-05-03", firstday = 5) # first day is Friday

# character coercion in yearweek format
as_yearweek("2019-W12", format = "yearweek")

# construction
yearweek(year = 2000, week = 3)

# direct construction
stopifnot(
    identical(
        new_yearweek(0:1, firstday = 1),
        as_yearweek("1969-12-29", firstday = 1) + 0:1
    )
)


grates documentation built on April 3, 2025, 10:49 p.m.