aweek-package {aweek} | R Documentation |
Convert dates to weeks and back again
Description
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.
-
get_week_start()
returns the global week_start option -
set_week_start()
sets the global week_start option
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 arounddate2week()
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.
-
get_aweek()
converts week numbers (with years and days) to aweek objects. -
get_date()
converts week numbers (with years and days) to Dates.
ISO week strings (YYYY-Www-d or YYYY-Www) to weeks or dates
-
as.aweek()
converts ISO-week formatted strings to aweek objects. -
week2date()
converts ISO-week formatted strings to Date.
aweek objects to dates or datetimes
This conversion is simple for aweek objects since their week_start is unambiguous
-
as.POSIXlt() converts to POSIXlt.
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.
Author(s)
Maintainer: Zhian N. Kamvar zkamvar@gmail.com
See Also
Useful links:
Report bugs at https://github.com/reconhub/aweek/issues/
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
# get the week numbers
date2week(d, numeric = TRUE)
# convert back to date
as.Date(w)
# convert to factor
factor_aweek(w)
# append a week
w[3] <- as.Date("2014-10-31")
w
# change week start variable (if needed)
change_week_start(w, "Monday")
# note that the date remains the same
as.Date(change_week_start(w, "Monday"))
# Don't forget to reset the week_start at the end
set_week_start(ow)