This week (2019-W24-2), the aweek package version 1.0.0 has been released on CRAN.

The aweek package is a response to the lack of utilities to easily define weeks that do not start on Monday, which is necessary for outbreak response in countries whose calendars may start on Sunday or Saturday. This package provides utilities to convert dates to weeks and back again with any week start definition.

You can install aweek with:

install.packages("aweek")

Using aweek is straightforward. When you load the package, you can set the day on which all weeks should start with set_week_start().

library("aweek")
# At the beginning of your analysis, set the week start to the weeks you want
# to use for reporting
ow <- set_week_start("Sunday")
d  <- as.Date("2019-06-11")
(w <- as.aweek(d)) # convert dates to weeks
## <aweek start: Sunday>
## [1] "2019-W24-3"
as.Date(w)         # convert weeks to dates
## [1] "2019-06-11"

All aweek objects carry a week_start attribute, which is the ISO week day that defines the start of the week for that object.

One of the common things you might want to do is to define a range of dates to use to filter linelist data. You can use get_date() to get a date from week numbers. Note that day in this context refers to the day of the week relative to the week start, so a “7” is “Saturday”.

library("dplyr")
library("tidyr")
linelist   <- as_tibble(outbreaks::ebola_sim_clean$linelist)
linelist
## # A tibble: 5,829 x 11
##    case_id generation date_of_infecti… date_of_onset date_of_hospita…
##    <chr>        <int> <date>           <date>        <date>          
##  1 d1fafd           0 NA               2014-04-07    2014-04-17      
##  2 53371b           1 2014-04-09       2014-04-15    2014-04-20      
##  3 f5c3d8           1 2014-04-18       2014-04-21    2014-04-25      
##  4 6c286a           2 NA               2014-04-27    2014-04-27      
##  5 0f58c4           2 2014-04-22       2014-04-26    2014-04-29      
##  6 49731d           0 2014-03-19       2014-04-25    2014-05-02      
##  7 f9149b           3 NA               2014-05-03    2014-05-04      
##  8 881bd4           3 2014-04-26       2014-05-01    2014-05-05      
##  9 e66fa4           2 NA               2014-04-21    2014-05-06      
## 10 20b688           3 NA               2014-05-05    2014-05-06      
## # … with 5,819 more rows, and 6 more variables: date_of_outcome <date>,
## #   outcome <fct>, gender <fct>, hospital <fct>, lon <dbl>, lat <dbl>
(obs_start <- get_date(week = 10, year = 2014, day = 1))
## [1] "2014-03-02"
(obs_end   <- get_date(week = 40, year = 2014, day = 7))
## [1] "2014-10-04"
ll_obs <- linelist %>%
  filter(date_of_onset >= obs_start) %>%
  filter(date_of_onset <= obs_end) %>%
  filter(hospital == "other")
ll_obs
## # A tibble: 344 x 11
##    case_id generation date_of_infecti… date_of_onset date_of_hospita…
##    <chr>        <int> <date>           <date>        <date>          
##  1 f5c3d8           1 2014-04-18       2014-04-21    2014-04-25      
##  2 0f58c4           2 2014-04-22       2014-04-26    2014-04-29      
##  3 e66fa4           2 NA               2014-04-21    2014-05-06      
##  4 2ae019           3 NA               2014-05-06    2014-05-11      
##  5 72b905           4 2014-05-06       2014-05-12    2014-05-13      
##  6 5fe599           4 2014-05-08       2014-05-13    2014-05-15      
##  7 dce5cc           4 2014-05-03       2014-05-17    2014-05-18      
##  8 d4ff47           6 2014-05-14       2014-05-17    2014-05-20      
##  9 49d786           3 NA               2014-05-20    2014-05-24      
## 10 76b97a           2 2014-05-11       2014-05-16    2014-05-25      
## # … with 334 more rows, and 6 more variables: date_of_outcome <date>,
## #   outcome <fct>, gender <fct>, hospital <fct>, lon <dbl>, lat <dbl>

You can use aweek to aggregate weekly data. The default aweek object is in the ISO 8601 format (YYYY-Www-d) and factor_aweek() can remove the -d portion, giving weekly aggregates, which will include any missing weeks in the levels:

ll_obs %>%
  mutate(week = as.aweek(date_of_onset)) %>%
  mutate(aggregate_week = factor_aweek(week)) %>%
  count(aggregate_week, gender, .drop = FALSE) %>%
  spread(gender, n)
## # A tibble: 24 x 3
##    aggregate_week     f     m
##    <fct>          <int> <int>
##  1 2014-W17           2     1
##  2 2014-W18           0     0
##  3 2014-W19           0     1
##  4 2014-W20           2     3
##  5 2014-W21           0     1
##  6 2014-W22           1     2
##  7 2014-W23           2     1
##  8 2014-W24           1     1
##  9 2014-W25           2     2
## 10 2014-W26           3     0
## # … with 14 more rows