Generic for conversion to <grates_yearweek>.
Usage
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"),
...
)
Arguments
- x
R object.
- ...
Other values passed to as.Date().
- firstday
[integer]
The day the week starts on from 1 (Monday) to 7 (Sunday).
- 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
Date, POSIXct, and POSIXlt are converted with the timezone respected.
Character objects are first coerced to date via
as.Date()
unlessformat = "yearweek"
in which case input is assumed to be in the form "YYYY-Wxx" and parsed accordingly.
See also
as.Date()
and new_yearweek()
.
Examples
as_yearweek(Sys.Date())
#> <grates_yearweek_monday[1]>
#> [1] "2024-W47"
as_yearweek(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"))
#> <grates_yearweek_monday[1]>
#> [1] "2019-W10"
as_yearweek("2019-05-03", firstday = 5L)
#> <grates_yearweek_friday[1]>
#> [1] "2019-W18"
as_yearweek("2019-W12", format = "yearweek")
#> <grates_yearweek_monday[1]>
#> [1] "2019-W12"