The aweek package is a lightweight solution for converting dates to weeks that can start on any weekday. It implements the aweek class, which can easily be converted to date and weeks that start on different days.

Before you begin

When you work with aweek, you will want to make sure that you set the default week_start variable to indicate which day of the week your weeks should begin. This can be done with set_week_start(). It will ensure that all of your weeks will begin on the same day.

Conversions

Dates to weeks

This conversion is the simplest because dates are unambiguous.

  • date2week() converts dates, datetimes, and characters that look like dates to weeks

  • as.aweek() is a wrapper around date2week() that converts dates and datetimes

Week numbers to weeks or dates

If you have separate columns for week numbers and years, then this is the option for you. This allows you to specify a different start for each week element using the start argument.

ISO week strings (YYYY-Www-d or YYYY-Www) to weeks or dates

aweek objects to dates or datetimes

This conversion is simple for aweek objects since their week_start is unambiguous

aweek objects to characters

You can strip the week_start attribute of the aweek object by converting to a character with as.character()

Manipulating aweek objects

  • trunc() removes the weekday element of the ISO week string.

  • factor_aweek() does the same thing as trunc(), but will create a factor with levels spanning all the weeks from the first week to the last week. Useful for creating tables with zero counts for unobserved weeks.

  • change_week_start() will change the week_start attribute and adjust the weeks accordingly so that the dates will always be consistent.

When you combine aweek objects, they must have the same week_start attribute. Characters can be added to aweek objects as long as they are in ISO week format and you can safely assume that they start on the same weekday. Dates are trivial to add to aweek objects. See the aweek documentation for details.

See also

Author

Maintainer: Zhian N. Kamvar zkamvar@gmail.com

Examples

# At the beginning of your analysis, set the week start to the weeks you want # to use for reporting ow <- set_week_start("Sunday") # convert dates to weeks d <- as.Date(c("2014-02-11", "2014-03-04")) w <- as.aweek(d) w
#> <aweek start: Sunday> #> [1] "2014-W07-3" "2014-W10-3"
# get the week numbers date2week(d, numeric = TRUE)
#> [1] 7 10
# convert back to date as.Date(w)
#> [1] "2014-02-11" "2014-03-04"
# convert to factor factor_aweek(w)
#> <aweek start: Sunday> #> [1] 2014-W07 2014-W10 #> Levels: 2014-W07 2014-W08 2014-W09 2014-W10
# append a week w[3] <- as.Date("2014-10-31") w
#> <aweek start: Sunday> #> [1] "2014-W07-3" "2014-W10-3" "2014-W44-6"
# change week start variable (if needed) change_week_start(w, "Monday")
#> <aweek start: Monday> #> [1] "2014-W07-2" "2014-W10-2" "2014-W44-5"
# note that the date remains the same as.Date(change_week_start(w, "Monday"))
#> [1] "2014-02-11" "2014-03-04" "2014-10-31"
# Don't forget to reset the week_start at the end set_week_start(ow)