Weeks differ in their start dates depending on context. The ISO
8601 standard specifies that Monday starts the week
(https://en.wikipedia.org/wiki/ISO_week_date) while the US CDC uses
Sunday as the start of the week
(https://wwwn.cdc.gov/nndss/document/MMWR_Week_overview.pdf). For
example, MSF has varying start dates depending on country in order to
better coordinate response.
While there are packages that provide conversion for ISOweeks and epiweeks,
these do not provide seamless conversion from dates to epiweeks with
non-standard start dates. This package provides a lightweight utility to
be able to convert each day.
Calculation of week numbers
Week numbers are calculated in three steps:
Find the day of the week, relative to the week_start (d). The day of the
week (d) relative to the week start (s) is calculated using the ISO week
day (i) via d = 1L + ((i + (7L - s)) %% 7L)
.
Find the date that represents midweek (m). The date that represents
midweek is found by subtracting the day of the week (d) from 4 and
adding that number of days to the current date: m = date + (4 - d)
.
Find the week number (w) by counting the number of days since 1 January
to (m), and use integer division by 7: w = 1L + ((m - yyyy-01-01) %/% 7)
For the weeks around 1 January, the year is determined by the week number.
If the month is January, but the week number is 52 or 53, then the year for
the week (YYYY) is the calendar year (yyyy) minus 1. However, if the month
is December, but the week number is 1, then the year for the week (YYYY) is
the calendar year (yyyy) plus 1.
Structure of the aweek object
The aweek object is a character vector in either the precise ISO week
format (YYYY-Www-d) or imprecise ISO week format (YYYY-Www) with
a week_start
attribute indicating which ISO week day the week begins.
The precise ISO week format can be broken down like this:
YYYY is an ISO week-numbering year, which is the year relative to
the week, not the day. For example, the date 2016-01-01 would be
represented as 2015-W53-5 (ISO week), because while the date is in the
year 2016, the week is still part of the final week of 2015.
Www is the week number, prefixed by the character "W". This ranges
from 01 to 52 or 53, depending on whether or not the year has 52 or 53
weeks.
d is a digit representing the weekday where 1 represents the first
day of the week and 7 represents the last day of the week. #'
The attribute week_start
represents the first day of the week as an ISO
week day. This defaults to 1, which is Monday. If, for example, an aweek
object represented weeks starting on Friday, then the week_start
attribute would be 5, which is Friday of the ISO week.
Imprecise formats (YYYY-Www) are equivalent to the first day of the week.
For example, 2015-W53 and 2015-W53-1 will be identical when converted to
date.