项目作者: reconhub

项目描述 :
Convert dates to arbitrary week definitions :calendar:
高级语言: R
项目地址: git://github.com/reconhub/aweek.git
创建时间: 2019-02-26T06:07:45Z
项目社区:https://github.com/reconhub/aweek

开源协议:Other

下载


CRAN\_Status\_Badge
Codecov test
coverage
Downloads
DOI
R-CMD-check

Welcome to the aweek package!

This package will convert dates to US CDC
epiweeks
,
isoweeks, and all others
in between with minimal overhead.

Installing the package

To install the stable package version from CRAN, you can use

  1. install.packages("aweek")

To benefit from the latest features and bug fixes, install the
development, github version of the package using:

  1. # install.packages("remotes")
  2. remotes::install_github("reconhub/aweek")

Main Features

  • date2week() converts dates to a week format (YYYY-Www-d) that can
    start on any day.
  • week2date() / 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.

Dates to weeks

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.

  1. # generate dates
  2. set.seed(2019-02-26)
  3. onset <- as.Date("2019-02-26") + sort(sample(-6:7, 20, replace = TRUE))
  4. # load aweek
  5. library("aweek")
  6. set_week_start("Monday") # set the default start of the week
  7. print(onset)
  1. ## [1] "2019-02-21" "2019-02-21" "2019-02-22" "2019-02-22" "2019-02-22" "2019-02-23" "2019-02-23" "2019-02-23" "2019-02-25" "2019-02-26" "2019-02-26" "2019-02-27" "2019-02-28" "2019-02-28" "2019-03-02" "2019-03-03"
  2. ## [17] "2019-03-04" "2019-03-05" "2019-03-05" "2019-03-05"
  1. date2week(onset) # convert dates to weeks
  1. ## <aweek start: Monday>
  2. ## [1] "2019-W08-4" "2019-W08-4" "2019-W08-5" "2019-W08-5" "2019-W08-5" "2019-W08-6" "2019-W08-6" "2019-W08-6" "2019-W09-1" "2019-W09-2" "2019-W09-2" "2019-W09-3" "2019-W09-4" "2019-W09-4" "2019-W09-6" "2019-W09-7"
  3. ## [17] "2019-W10-1" "2019-W10-2" "2019-W10-2" "2019-W10-2"

If you want to override the default, you can use the week_start
attribute of date2week():

  1. date2week(onset, week_start = 1) # ISO weeks starting on Monday (default)
  1. ## <aweek start: Monday>
  2. ## [1] "2019-W08-4" "2019-W08-4" "2019-W08-5" "2019-W08-5" "2019-W08-5" "2019-W08-6" "2019-W08-6" "2019-W08-6" "2019-W09-1" "2019-W09-2" "2019-W09-2" "2019-W09-3" "2019-W09-4" "2019-W09-4" "2019-W09-6" "2019-W09-7"
  3. ## [17] "2019-W10-1" "2019-W10-2" "2019-W10-2" "2019-W10-2"
  1. date2week(onset, week_start = 7) # EPI week starting on Sunday
  1. ## <aweek start: Sunday>
  2. ## [1] "2019-W08-5" "2019-W08-5" "2019-W08-6" "2019-W08-6" "2019-W08-6" "2019-W08-7" "2019-W08-7" "2019-W08-7" "2019-W09-2" "2019-W09-3" "2019-W09-3" "2019-W09-4" "2019-W09-5" "2019-W09-5" "2019-W09-7" "2019-W10-1"
  3. ## [17] "2019-W10-2" "2019-W10-3" "2019-W10-3" "2019-W10-3"
  1. date2week(onset, week_start = 6) # EPI week starting on Saturday
  1. ## <aweek start: Saturday>
  2. ## [1] "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"
  3. ## [17] "2019-W10-3" "2019-W10-4" "2019-W10-4" "2019-W10-4"
  1. x <- date2week(onset, week_start = "sat", floor_day = TRUE) # truncate to just the weeks
  2. table(x)
  1. ## x
  2. ## 2019-W08 2019-W09 2019-W10
  3. ## 5 9 6
  1. table(week2date(x))
  1. ##
  2. ## 2019-02-16 2019-02-23 2019-03-02
  3. ## 5 9 6

Weeks to dates

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:

  1. get_date(week = 44:53, year = 2015)
  1. ## [1] "2015-10-26" "2015-11-02" "2015-11-09" "2015-11-16" "2015-11-23" "2015-11-30" "2015-12-07" "2015-12-14" "2015-12-21" "2015-12-28"
  1. # you can also use this to generate aweek objects
  2. get_aweek(week = 44:53, year = 2015)
  1. ## <aweek start: Monday>
  2. ## [1] "2015-W44-1" "2015-W45-1" "2015-W46-1" "2015-W47-1" "2015-W48-1" "2015-W49-1" "2015-W50-1" "2015-W51-1" "2015-W52-1" "2015-W53-1"

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

  1. get_date(week = 1, year = 2018, day = 1, start = c("Sunday", "Monday"))
  1. ## [1] "2017-12-31" "2018-01-01"
  1. # get_aweek will align them to the default week_start
  2. get_aweek(week = 1, year = 2018, day = 1, start = c("Sunday", "Monday"))
  1. ## <aweek start: Monday>
  2. ## [1] "2017-W52-7" "2018-W01-1"

Factors

You can also automatically calculate factor levels, which is useful in
tabulating across weeks and including missing values.

  1. set.seed(2019-02-26)
  2. onset <- as.Date("2019-02-26") + sort(sample(-48:49, 20, replace = TRUE))
  3. x <- date2week(onset, week_start = "sat", factor = TRUE)
  4. x
  1. ## <aweek start: Saturday>
  2. ## [1] 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
  3. ## 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
  1. table(x)
  1. ## x
  2. ## 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
  3. ## 2 3 3 3 0 0 2 1 1 2 0 1 2

Locales

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.

  1. # workaround because of differing locale specifications
  2. german <- if (grepl("darwin", R.version$os)) "de_DE.UTF-8" else "de_DE.utf8"
  3. lct <- Sys.getlocale("LC_TIME")
  4. res <- Sys.setlocale("LC_TIME", german)
  5. date2week(as.Date("2019-02-26"), week_start = "Sonntag")
  1. ## <aweek start: Sonntag>
  2. ## [1] "2019-W09-3"
  1. date2week(as.Date("2019-02-26"), week_start = "Sunday")
  1. ## <aweek start: Sonntag>
  2. ## [1] "2019-W09-3"
  1. Sys.setlocale("LC_TIME", lct)
  1. ## [1] "en_US.UTF-8"

Getting help online

Bug reports and feature requests should be posted on github using the
issue system. All other
questions should be posted on the RECON forum:

https://www.repidemicsconsortium.org/forum

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.

Similar Work

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.

  • ISOweek converts dates
    to ISO weeks as the “%W” and “%u” formats don’t exist in windows
  • lubridate performs general
    datetime handling with some auxiliary functions that return the week
    or day of the week.
  • surveillance
    implements ISOWeekYear.