Learn R Programming

grates (version 1.4.3)

epiweek_class: Epiweek class

Description

Epiweeks are defined to start on a Sunday and span a 7 day period. Where they span calendar years, they are associated to the year which contains the majority of the week's days (i.e. the first epiweek a year is the one with at least four days in said year).

Internally, <grates_epiweek> objects are stored as the number of weeks (starting at 0) from the first Sunday after the Unix Epoch (1970-01-01). That is, the number of seven day periods from 1970-01-04.

Usage

epiweek(year = integer(), week = integer())

as_epiweek(x, ...)

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

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

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

# S3 method for character as_epiweek(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"), ...)

# S3 method for factor as_epiweek(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"), ...)

new_epiweek(x = integer())

is_epiweek(xx)

Value

A <grates_epiweek> object.

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

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

epiweek() is a constructor for <grates_epiweek> objects. 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_epiweek() is a generic for conversion to <grates_epiweek>.

  • 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_epiweek() is a minimal constructor for <grates_epiweek> objects aimed at developers. It takes, as input, the number of epiweeks since the sunday after the Unix Epoch that you wish to represent. double vectors will be converted to integer via as.integer(floor(x)).

See Also

The yearweek and isoweek classes.

Examples

Run this code

# date coercion
as_epiweek(Sys.Date())

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

# character coercion assumes date input by default
as_epiweek("2019-05-03")

# character coercion can handle YYYY-Www format too
as_epiweek("2019-W12", format = "yearweek")

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

# direct construction
stopifnot(
    identical(
        new_epiweek(0:1),
        as_epiweek("1970-01-04") + 0:1
    )
)

Run the code above in your browser using DataLab