aweek-package: Convert dates to weeks and back again

Description Before you begin Conversions Manipulating aweek objects Author(s) See Also Examples

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.

Conversions

Dates to weeks

This conversion is the simplest because dates are unambiguous.

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

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:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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)

reconhub/aweek documentation built on March 25, 2021, 12:03 a.m.