date2week()converts dates to a week format (YYYY-Www-d) that can start on any day.
as.Date()does the backwards conversion from (YYYY-Www(-d)) to a numeric date.
get_aweek()generates an aweek object from a week number
get_date()converts a week number to a date
as.aweek()converts dates, characters, and factors to aweek objects.
factor_aweek()creates an aggregated factor of weeks where the levels contain all weeks within range.
- Dependencies only on R itself.
With the aweek package, converting dates to weeks is simple. All you need to know is what weekday represents the beginning of your week and a vector of dates.
date2week(onset) # convert dates to weeks
If you want to override the default, you can use the
week_start attribute of
date2week(onset, week_start = 1) # ISO weeks starting on Monday (default)
date2week(onset, week_start = 7) # EPI week starting on Sunday
date2week(onset, week_start = 6) # EPI week starting on Saturday
## <aweek start: Saturday> ##  "2019-W08-6" "2019-W08-6" "2019-W08-7" "2019-W08-7" "2019-W08-7" "2019-W09-1" "2019-W09-1" "2019-W09-1" "2019-W09-3" "2019-W09-4" "2019-W09-4" "2019-W09-5" "2019-W09-6" "2019-W09-6" "2019-W10-1" "2019-W10-2" ##  "2019-W10-3" "2019-W10-4" "2019-W10-4" "2019-W10-4"
If you have numeric weeks, you can rapidly convert to dates with
get_date(). Here are all the dates for the first day of last 10 ISO weeks of 2015:
get_date(week = 44:53, year = 2015)
# you can also use this to generate aweek objects get_aweek(week = 44:53, year = 2015)
If you have weeks recorded from different data sets that start on different days, you can account for that by using the
start option. For example, 2018-01-01 is a Monday, but 2018-W01-1 starting on a Sunday is 2017-12-31
You can also automatically calculate factor levels, which is useful in tabulating across weeks and including missing values.
## <aweek start: Saturday> ##  2019-W03 2019-W03 2019-W04 2019-W04 2019-W04 2019-W05 2019-W05 2019-W05 2019-W06 2019-W06 2019-W06 2019-W09 2019-W09 2019-W10 2019-W11 2019-W12 2019-W12 2019-W14 2019-W15 2019-W15 ## Levels: 2019-W03 2019-W04 2019-W05 2019-W06 2019-W07 2019-W08 2019-W09 2019-W10 2019-W11 2019-W12 2019-W13 2019-W14 2019-W15
It’s also possible to specify the week_start in terms of the current locale, however it is important to be aware that code like this may not be portable.
# workaround because of differing locale specifications german <- if (grepl("darwin", R.version$os)) "de_DE.UTF-8" else "de_DE.utf8" lct <- Sys.getlocale("LC_TIME") res <- Sys.setlocale("LC_TIME", german) date2week(as.Date("2019-02-26"), week_start = "Sonntag")
Contributions are welcome via pull requests.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
There are other packages that can define ISOweeks and/or epiweeks. However, the ability to easily switch between day and week intervals is only available for the ISOweek package and all of the packages above have dependencies that require compiled code.