as.aweek | R Documentation |
Convert characters or dates to aweek objects
as.aweek(x, week_start = get_week_start(), ...) ## Default S3 method: as.aweek(x, week_start = NULL, ...) ## S3 method for class ''NULL'' as.aweek(x, week_start = NULL, ...) ## S3 method for class 'character' as.aweek(x, week_start = get_week_start(), start = week_start, ...) ## S3 method for class 'factor' as.aweek(x, week_start = get_week_start(), ...) ## S3 method for class 'Date' as.aweek(x, week_start = get_week_start(), ...) ## S3 method for class 'POSIXt' as.aweek(x, week_start = get_week_start(), ...) ## S3 method for class 'aweek' as.aweek(x, week_start = NULL, ...)
x |
a Date, POSIXct, POSIXlt, or a correctly formatted (YYYY-Www-d) character string that represents the year, week, and weekday. |
week_start |
a number indicating the start of the week based on the ISO
8601 standard from 1 to 7 where 1 = Monday OR an abbreviation of the
weekdate in an English or current locale. Note: using a non-English locale
may render your code non-portable. Defaults to the value of
|
... |
arguments passed on to |
start |
an integer (or character) vector of days that the weeks
start on for each corresponding week. Defaults to the value of
|
The as.aweek()
will coerce character, dates, and datetime objects
to aweek objects. Dates are trivial to convert to weeks because there is
only one correct way to convert them with any given week_start
.
There is a bit of nuance to be aware of when converting characters to aweek objects:
The characters must be correctly formatted as YYYY-Www-d
, where YYYY
is the year relative to the week, Www is the week number (ww) prepended
by a W, and d (optional) is the day of the week from 1 to 7 where 1
represents the week_start. This means that characters formatted as
dates will be rejected.
By default, the week_start
and start
parameters are identical. If
your data contains heterogeneous weeks (e.g. some dates will have the
week start on Monday and some will have the week start on Sunday), then
you should use the start
parameter to reflect this. Internally, the
weeks will first be converted to dates with their respective starts and
then converted back to weeks, unified under the week_start
parameter.
an aweek object
factors are first converted to characters before they are converted to aweek objects.
"aweek-class" for details on the aweek object,
get_aweek()
for converting numeric weeks to weeks or dates,
date2week()
for converting dates to weeks, week2date()
for converting
weeks to dates.
# aweek objects can only be created from valid weeks: as.aweek("2018-W10-5", week_start = 7) # works! try(as.aweek("2018-10-5", week_start = 7)) # doesn't work :( # you can also convert dates or datetimes as.aweek(Sys.Date()) as.aweek(Sys.time()) # all functions get passed to date2week, so you can use any of its arguments: as.aweek("2018-W10-5", week_start = 7, floor_day = TRUE, factor = TRUE) as.aweek(as.Date("2018-03-09"), floor_day = TRUE, factor = TRUE) # If you have a character vector where different elements begin on different # days of the week, you can use the "start" argument to ensure they are # correctly converted. as.aweek(c(mon = "2018-W10-1", tue = "2018-W10-1"), week_start = "Monday", start = c("Monday", "Tuesday")) # you can convert aweek objects to aweek objects: x <- get_aweek() as.aweek(x) as.aweek(x, week_start = 7)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.