| Title: | Date and Time Functions for Public Health Purposes |
|---|---|
| Description: | Provides easy and consistent time conversion for public health purposes. The time conversion functions provided here are between date, ISO week, ISO yearweek, ISO year, calendar month/year, season, season week. |
| Authors: | Chi Zhang [aut] (ORCID: <https://orcid.org/0000-0003-0501-5909>), Richard Aubrey White [aut, cre] (ORCID: <https://orcid.org/0000-0002-6747-1726>) |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2025.10.13 |
| Built: | 2026-07-01 09:39:27 UTC |
| Source: | https://github.com/niphr/cstime |
Converts a date to its calendar month number, returned as a zero-padded character string.
date_to_calmonth_c(x = lubridate::today()) ## Default S3 method: date_to_calmonth_c(x) ## S3 method for class 'character' date_to_calmonth_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calmonth_c(x = lubridate::today())date_to_calmonth_c(x = lubridate::today()) ## Default S3 method: date_to_calmonth_c(x) ## S3 method for class 'character' date_to_calmonth_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calmonth_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The month is returned as two digits, "01" for January through "12" for December.
Calendar month as a character vector ("01" to "12").
date_to_calmonth_c(as.Date("2021-08-11")) date_to_calmonth_c("2021-01-01")date_to_calmonth_c(as.Date("2021-08-11")) date_to_calmonth_c("2021-01-01")
Converts a date to its calendar month number, returned as a number.
date_to_calmonth_n(x = lubridate::today()) ## Default S3 method: date_to_calmonth_n(x) ## S3 method for class 'character' date_to_calmonth_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_calmonth_n(x = lubridate::today())date_to_calmonth_n(x = lubridate::today()) ## Default S3 method: date_to_calmonth_n(x) ## S3 method for class 'character' date_to_calmonth_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_calmonth_n(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
Calendar month as an integer vector (1 for January to 12 for December).
date_to_calmonth_n(as.Date("2021-08-11")) date_to_calmonth_n("2021-01-01")date_to_calmonth_n(as.Date("2021-08-11")) date_to_calmonth_n("2021-01-01")
Converts a date to its calendar (Gregorian) year, returned as a character string.
date_to_calyear_c(x = lubridate::today()) ## Default S3 method: date_to_calyear_c(x) ## S3 method for class 'character' date_to_calyear_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyear_c(x = lubridate::today())date_to_calyear_c(x = lubridate::today()) ## Default S3 method: date_to_calyear_c(x) ## S3 method for class 'character' date_to_calyear_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyear_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
Unlike the ISO year (see date_to_isoyear_c()), the calendar year is taken
directly from the date, so 2021-01-01 is calendar year "2021".
Calendar year as a character vector (e.g. "2021").
date_to_calyear_c(as.Date("2021-08-11")) date_to_calyear_c("2021-01-01")date_to_calyear_c(as.Date("2021-08-11")) date_to_calyear_c("2021-01-01")
Converts a date to its calendar (Gregorian) year, returned as a number.
date_to_calyear_n(x = lubridate::today()) ## Default S3 method: date_to_calyear_n(x) ## S3 method for class 'character' date_to_calyear_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyear_n(x = lubridate::today())date_to_calyear_n(x = lubridate::today()) ## Default S3 method: date_to_calyear_n(x) ## S3 method for class 'character' date_to_calyear_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyear_n(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
Unlike the ISO year (see date_to_isoyear_n()), the calendar year is taken
directly from the date, so 2021-01-01 is calendar year 2021.
Calendar year as an integer vector (e.g. 2021).
date_to_calyear_n(as.Date("2021-08-11")) date_to_calyear_n("2021-01-01")date_to_calyear_n(as.Date("2021-08-11")) date_to_calyear_n("2021-01-01")
Converts a date to a combined calendar year and month string of the form "yyyy-Mmm".
date_to_calyearmonth_c(x = lubridate::today()) ## Default S3 method: date_to_calyearmonth_c(x) ## S3 method for class 'character' date_to_calyearmonth_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyearmonth_c(x = lubridate::today())date_to_calyearmonth_c(x = lubridate::today()) ## Default S3 method: date_to_calyearmonth_c(x) ## S3 method for class 'character' date_to_calyearmonth_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_calyearmonth_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The output combines the calendar year and the zero-padded calendar month, separated by "-M", for example "2021-M08".
Calendar yearmonth as a character vector (e.g. "2021-M08").
date_to_calyearmonth_c(as.Date("2021-08-11")) date_to_calyearmonth_c("2021-01-01")date_to_calyearmonth_c(as.Date("2021-08-11")) date_to_calyearmonth_c("2021-01-01")
Converts a date to an ISO-week-based quarter (1 to 4), returned as a character string.
date_to_isoquarter_c(x = lubridate::today()) ## Default S3 method: date_to_isoquarter_c(x) ## S3 method for class 'character' date_to_isoquarter_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoquarter_c(x = lubridate::today())date_to_isoquarter_c(x = lubridate::today()) ## Default S3 method: date_to_isoquarter_c(x) ## S3 method for class 'character' date_to_isoquarter_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoquarter_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The quarter is derived from the ISO week. See date_to_isoquarter_n() for
the week-to-quarter boundaries.
ISO quarter as a character vector (e.g. "3").
date_to_isoquarter_c(as.Date("2021-08-11")) date_to_isoquarter_c("2021-01-01")date_to_isoquarter_c(as.Date("2021-08-11")) date_to_isoquarter_c("2021-01-01")
Converts a date to an ISO-week-based quarter (1 to 4), returned as a number.
date_to_isoquarter_n(x = lubridate::today()) ## Default S3 method: date_to_isoquarter_n(x) ## S3 method for class 'character' date_to_isoquarter_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoquarter_n(x = lubridate::today())date_to_isoquarter_n(x = lubridate::today()) ## Default S3 method: date_to_isoquarter_n(x) ## S3 method for class 'character' date_to_isoquarter_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoquarter_n(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The quarter is derived from the ISO week rather than the calendar month: weeks 1 to 13 are quarter 1, weeks 14 to 26 are quarter 2, weeks 27 to 39 are quarter 3, and weeks 40 onwards (including week 53 in long ISO years) are quarter 4.
ISO quarter as an integer vector (1 to 4).
date_to_isoquarter_n(as.Date("2021-08-11")) date_to_isoquarter_n("2021-01-01")date_to_isoquarter_n(as.Date("2021-08-11")) date_to_isoquarter_n("2021-01-01")
Converts a date to its ISO 8601 week number, returned as a zero-padded character string.
date_to_isoweek_c(x = lubridate::today()) ## Default S3 method: date_to_isoweek_c(x) ## S3 method for class 'character' date_to_isoweek_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoweek_c(x = lubridate::today())date_to_isoweek_c(x = lubridate::today()) ## Default S3 method: date_to_isoweek_c(x) ## S3 method for class 'character' date_to_isoweek_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoweek_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
ISO weeks run Monday to Sunday and are numbered 01 to 52 or 53. Week 01 is the week containing the first Thursday of the ISO year. The week is returned as two digits, e.g. "01" or "53".
ISO week as a character vector (e.g. "32").
date_to_isoweek_c(as.Date("2021-08-11")) date_to_isoweek_c("2021-01-01")date_to_isoweek_c(as.Date("2021-08-11")) date_to_isoweek_c("2021-01-01")
Converts a date to its ISO 8601 week number, returned as a number.
date_to_isoweek_n(x = lubridate::today()) ## Default S3 method: date_to_isoweek_n(x) ## S3 method for class 'character' date_to_isoweek_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoweek_n(x = lubridate::today())date_to_isoweek_n(x = lubridate::today()) ## Default S3 method: date_to_isoweek_n(x) ## S3 method for class 'character' date_to_isoweek_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoweek_n(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
ISO weeks run Monday to Sunday and are numbered 1 to 52 or 53. Week 1 is the week containing the first Thursday of the ISO year.
ISO week as an integer vector (1 to 53).
date_to_isoweek_n(as.Date("2021-08-11")) date_to_isoweek_n("2021-01-01")date_to_isoweek_n(as.Date("2021-08-11")) date_to_isoweek_n("2021-01-01")
Converts a date to its ISO 8601 week-based year, returned as a character string.
date_to_isoyear_c(x = lubridate::today()) ## Default S3 method: date_to_isoyear_c(x) ## S3 method for class 'character' date_to_isoyear_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyear_c(x = lubridate::today())date_to_isoyear_c(x = lubridate::today()) ## Default S3 method: date_to_isoyear_c(x) ## S3 method for class 'character' date_to_isoyear_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyear_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The ISO 8601 week-based year is not always the same as the calendar year. ISO weeks run Monday to Sunday, and week 1 is the week containing the year's first Thursday. As a result the first days of January can belong to the last ISO week of the previous year, and the last days of December can belong to ISO week 1 of the following year. For example 2021-01-01 is a Friday that falls in ISO week 53 of ISO year 2020.
ISO year as a character vector (e.g. "2021").
date_to_isoyear_c(as.Date("2021-08-11")) date_to_isoyear_c("2021-01-01")date_to_isoyear_c(as.Date("2021-08-11")) date_to_isoyear_c("2021-01-01")
Converts a date to its ISO 8601 week-based year, returned as a number.
date_to_isoyear_n(x = lubridate::today()) ## Default S3 method: date_to_isoyear_n(x) ## S3 method for class 'character' date_to_isoyear_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyear_n(x = lubridate::today())date_to_isoyear_n(x = lubridate::today()) ## Default S3 method: date_to_isoyear_n(x) ## S3 method for class 'character' date_to_isoyear_n(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyear_n(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The ISO 8601 week-based year can differ from the calendar year near the
start and end of January and December. See date_to_isoyear_c() for the
rules used to assign weeks and years.
ISO year as an integer vector (e.g. 2021).
date_to_isoyear_n(as.Date("2021-08-11")) date_to_isoyear_n("2021-01-01")date_to_isoyear_n(as.Date("2021-08-11")) date_to_isoyear_n("2021-01-01")
Converts a date to a combined ISO year and quarter string of the form "yyyy-Qn".
date_to_isoyearquarter_c(x = lubridate::today()) ## Default S3 method: date_to_isoyearquarter_c(x) ## S3 method for class 'character' date_to_isoyearquarter_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyearquarter_c(x = lubridate::today())date_to_isoyearquarter_c(x = lubridate::today()) ## Default S3 method: date_to_isoyearquarter_c(x) ## S3 method for class 'character' date_to_isoyearquarter_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyearquarter_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The output combines the ISO year (see date_to_isoyear_c()) and the
ISO-week-based quarter (see date_to_isoquarter_c()), for example
"2021-Q3".
ISO yearquarter as a character vector (e.g. "2021-Q3").
date_to_isoyearquarter_c(as.Date("2021-08-11")) date_to_isoyearquarter_c("2021-01-01")date_to_isoyearquarter_c(as.Date("2021-08-11")) date_to_isoyearquarter_c("2021-01-01")
Converts a date to a combined ISO 8601 year and week string of the form "yyyy-ww".
date_to_isoyearweek_c(x = lubridate::today()) ## Default S3 method: date_to_isoyearweek_c(x) ## S3 method for class 'character' date_to_isoyearweek_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyearweek_c(x = lubridate::today())date_to_isoyearweek_c(x = lubridate::today()) ## Default S3 method: date_to_isoyearweek_c(x) ## S3 method for class 'character' date_to_isoyearweek_c(x = lubridate::today()) ## S3 method for class 'Date' date_to_isoyearweek_c(x = lubridate::today())
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The output combines the ISO year (see date_to_isoyear_c()) and the
zero-padded ISO week (see date_to_isoweek_c()), separated by a hyphen, for
example "2021-32". Because the ISO year can differ from the calendar year,
2021-01-01 maps to "2020-53".
ISO yearweek as a character vector (e.g. "2021-32").
date_to_isoyearweek_c(as.Date("2021-08-11")) date_to_isoyearweek_c("2021-01-01")date_to_isoyearweek_c(as.Date("2021-08-11")) date_to_isoyearweek_c("2021-01-01")
Maps a date to the surveillance season it belongs to, written as "yyyy/yyyy".
date_to_season_c(x)date_to_season_c(x)
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The date is first converted to an ISO yearweek with
date_to_isoyearweek_c() and then to a season with
isoyearweek_to_season_c(). Seasons start at ISO week 35, so dates in early
January belong to the season that began the previous calendar year.
Season as a character vector (e.g. "2020/2021").
date_to_season_c(c("2021-01-01", "2021-12-01")) date_to_season_c(as.Date("2021-09-01"))date_to_season_c(c("2021-01-01", "2021-12-01")) date_to_season_c(as.Date("2021-09-01"))
Maps a date to its position within the surveillance season, where season week 1 corresponds to ISO week 35.
date_to_seasonweek_n(x)date_to_seasonweek_n(x)
x |
A Date object, or a character string in the format 'yyyy-mm-dd'. |
The date is first converted to an ISO yearweek with
date_to_isoyearweek_c() and then to a season week with
isoyearweek_to_seasonweek_n(). As with isoweek_to_seasonweek_n(), ISO
week 53 maps to the half-step season week 18.5.
Season week as a numeric vector.
date_to_seasonweek_n(c("2021-01-01", "2021-12-01")) date_to_seasonweek_n(as.Date("2021-09-01"))date_to_seasonweek_n(c("2021-01-01", "2021-12-01")) date_to_seasonweek_n(as.Date("2021-09-01"))
A lookup table giving, for every ISO yearweek between 1900 and 2100, the date of each day of that week (Monday to Sunday) plus convenience list-columns that group those dates.
dates_by_isoyearweekdates_by_isoyearweek
A data.table::data.table with one row per ISO yearweek and the following columns:
Integer. ISO year.
Character. ISO yearweek, "yyyy-ww".
Date of Monday.
Date of Tuesday.
Date of Wednesday.
Date of Thursday.
Date of Friday.
Date of Saturday.
Date of Sunday.
List column. The dates Monday to Friday.
List column. The dates Saturday and Sunday.
List column. The dates Monday to Sunday.
Generated from a daily date sequence using ISO 8601 week rules. See
data-raw/1_gen-data.R in the package source.
library(data.table) # Look up the Monday and Sunday of two ISO yearweeks dates_by_isoyearweek[isoyearweek %in% c("2021-01", "2021-02"), .(isoyearweek, mon, sun)] # Constructing a vector of dates without removing the Date class do.call("c", dates_by_isoyearweek[isoyearweek %in% c("2021-01", "2021-02")]$weekdays)library(data.table) # Look up the Monday and Sunday of two ISO yearweeks dates_by_isoyearweek[isoyearweek %in% c("2021-01", "2021-02"), .(isoyearweek, mon, sun)] # Constructing a vector of dates without removing the Date class do.call("c", dates_by_isoyearweek[isoyearweek %in% c("2021-01", "2021-02")]$weekdays)
Maps an ISO week number to its position within the surveillance season, where season week 1 corresponds to ISO week 35.
isoweek_to_seasonweek_n(x) ## Default S3 method: isoweek_to_seasonweek_n(x) ## S3 method for class 'character' isoweek_to_seasonweek_n(x) ## S3 method for class 'numeric' isoweek_to_seasonweek_n(x)isoweek_to_seasonweek_n(x) ## Default S3 method: isoweek_to_seasonweek_n(x) ## S3 method for class 'character' isoweek_to_seasonweek_n(x) ## S3 method for class 'numeric' isoweek_to_seasonweek_n(x)
x |
ISO week as a number between 1 and 53. |
Surveillance seasons start at ISO week 35, so ISO week 35 is season week 1, ISO week 36 is season week 2, and so on, wrapping around the new year. ISO week 53 (which only occurs in long ISO years) maps to the half-step season week 18.5 so that the surrounding weeks keep consistent numbering.
Season week as a numeric vector (ISO week 53 returns 18.5).
isoweek_to_seasonweek_n(35) isoweek_to_seasonweek_n(c(31, 53))isoweek_to_seasonweek_n(35) isoweek_to_seasonweek_n(c(31, 53))
Returns the date of the Sunday that ends the last ISO week of a given ISO year.
isoyear_to_last_date(x) ## Default S3 method: isoyear_to_last_date(x) ## S3 method for class 'character' isoyear_to_last_date(x) ## S3 method for class 'numeric' isoyear_to_last_date(x)isoyear_to_last_date(x) ## Default S3 method: isoyear_to_last_date(x) ## S3 method for class 'character' isoyear_to_last_date(x) ## S3 method for class 'numeric' isoyear_to_last_date(x)
x |
ISO year as a number or character string, e.g. 2020 or "2020". |
ISO weeks end on Sunday, so the returned date is the Sunday of the final ISO week. Because ISO years and calendar years are not aligned, this date can fall in early January of the following calendar year (for example the last date of ISO year 2020 is 2021-01-03). The year is accepted as either a number or a character string.
A base::Date vector giving the last Sunday of each ISO year.
isoyear_to_last_date(c(2019, 2020, 2021)) isoyear_to_last_date("2020")isoyear_to_last_date(c(2019, 2020, 2021)) isoyear_to_last_date("2020")
Returns the number of the last ISO week in a given ISO year, that is, the count of ISO weeks in that year.
isoyear_to_last_isoweek_n(x) ## Default S3 method: isoyear_to_last_isoweek_n(x) ## S3 method for class 'character' isoyear_to_last_isoweek_n(x) ## S3 method for class 'numeric' isoyear_to_last_isoweek_n(x)isoyear_to_last_isoweek_n(x) ## Default S3 method: isoyear_to_last_isoweek_n(x) ## S3 method for class 'character' isoyear_to_last_isoweek_n(x) ## S3 method for class 'numeric' isoyear_to_last_isoweek_n(x)
x |
ISO year as a number or character string, e.g. 2020 or "2020". |
This is 52 for most years and 53 for long ISO years such as 2020. The year is accepted as either a number or a character string.
Last ISO week of the year as an integer vector (52 or 53).
isoyear_to_last_isoweek_n(c(2019, 2020, 2021)) isoyear_to_last_isoweek_n("2020")isoyear_to_last_isoweek_n(c(2019, 2020, 2021)) isoyear_to_last_isoweek_n("2020")
Returns the last ISO yearweek of a given ISO year as a "yyyy-ww" string.
isoyear_to_last_isoyearweek_c(x) ## Default S3 method: isoyear_to_last_isoyearweek_c(x) ## S3 method for class 'character' isoyear_to_last_isoyearweek_c(x) ## S3 method for class 'numeric' isoyear_to_last_isoyearweek_c(x)isoyear_to_last_isoyearweek_c(x) ## Default S3 method: isoyear_to_last_isoyearweek_c(x) ## S3 method for class 'character' isoyear_to_last_isoyearweek_c(x) ## S3 method for class 'numeric' isoyear_to_last_isoyearweek_c(x)
x |
ISO year as a number or character string, e.g. 2020 or "2020". |
Most ISO years have 52 weeks, so the result is usually "yyyy-52". ISO years that contain 53 weeks (such as 2020) instead return "yyyy-53". The year is accepted as either a number or a character string.
Last ISO yearweek of the year as a character vector (e.g. "2020-53").
isoyear_to_last_isoyearweek_c(c(2019, 2020, 2021)) isoyear_to_last_isoyearweek_c("2020")isoyear_to_last_isoyearweek_c(c(2019, 2020, 2021)) isoyear_to_last_isoyearweek_c("2020")
Maps an ISO yearweek to its ISO-week-based quarter (1 to 4), returned as a character string.
isoyearweek_to_isoquarter_c(x) ## Default S3 method: isoyearweek_to_isoquarter_c(x) ## S3 method for class 'character' isoyearweek_to_isoquarter_c(x)isoyearweek_to_isoquarter_c(x) ## Default S3 method: isoyearweek_to_isoquarter_c(x) ## S3 method for class 'character' isoyearweek_to_isoquarter_c(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The quarter is derived from the ISO week part of the input. See
isoyearweek_to_isoquarter_n() for the week-to-quarter boundaries.
ISO quarter as a character vector (e.g. "2").
isoyearweek_to_isoquarter_c("2020-19")isoyearweek_to_isoquarter_c("2020-19")
Maps an ISO yearweek to its ISO-week-based quarter (1 to 4), returned as a number.
isoyearweek_to_isoquarter_n(x) ## Default S3 method: isoyearweek_to_isoquarter_n(x) ## S3 method for class 'character' isoyearweek_to_isoquarter_n(x)isoyearweek_to_isoquarter_n(x) ## Default S3 method: isoyearweek_to_isoquarter_n(x) ## S3 method for class 'character' isoyearweek_to_isoquarter_n(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The quarter is derived from the ISO week part of the input. Weeks 1 to 13 are quarter 1, weeks 14 to 26 are quarter 2, weeks 27 to 39 are quarter 3, and weeks 40 onwards (including week 53) are quarter 4.
ISO quarter as an integer vector (1 to 4).
isoyearweek_to_isoquarter_n("2020-19")isoyearweek_to_isoquarter_n("2020-19")
Extracts the ISO week from an ISO yearweek string and returns it as a character string.
isoyearweek_to_isoweek_c(x) ## Default S3 method: isoyearweek_to_isoweek_c(x) ## S3 method for class 'character' isoyearweek_to_isoweek_c(x)isoyearweek_to_isoweek_c(x) ## Default S3 method: isoyearweek_to_isoweek_c(x) ## S3 method for class 'character' isoyearweek_to_isoweek_c(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The input is split on the hyphen into year and week, and the week part is returned. The year part is ignored.
ISO week as a character vector (e.g. "19").
isoyearweek_to_isoweek_c("2020-19")isoyearweek_to_isoweek_c("2020-19")
Extracts the ISO week from an ISO yearweek string and returns it as a number.
isoyearweek_to_isoweek_n(x) ## Default S3 method: isoyearweek_to_isoweek_n(x) ## S3 method for class 'character' isoyearweek_to_isoweek_n(x)isoyearweek_to_isoweek_n(x) ## Default S3 method: isoyearweek_to_isoweek_n(x) ## S3 method for class 'character' isoyearweek_to_isoweek_n(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The input is split on the hyphen into year and week, and the week part is returned. The year part is ignored.
ISO week as an integer vector (1 to 53).
isoyearweek_to_isoweek_n("2020-19")isoyearweek_to_isoweek_n("2020-19")
Extracts the ISO year from an ISO yearweek string and returns it as a character string.
isoyearweek_to_isoyear_c(x) ## Default S3 method: isoyearweek_to_isoyear_c(x) ## S3 method for class 'character' isoyearweek_to_isoyear_c(x)isoyearweek_to_isoyear_c(x) ## Default S3 method: isoyearweek_to_isoyear_c(x) ## S3 method for class 'character' isoyearweek_to_isoyear_c(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The input is split on the hyphen into year and week, and the year part is returned. The week part is ignored.
ISO year as a character vector (e.g. "2020").
isoyearweek_to_isoyear_c("2020-10")isoyearweek_to_isoyear_c("2020-10")
Extracts the ISO year from an ISO yearweek string and returns it as a number.
isoyearweek_to_isoyear_n(x) ## Default S3 method: isoyearweek_to_isoyear_n(x) ## S3 method for class 'character' isoyearweek_to_isoyear_n(x)isoyearweek_to_isoyear_n(x) ## Default S3 method: isoyearweek_to_isoyear_n(x) ## S3 method for class 'character' isoyearweek_to_isoyear_n(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The input is split on the hyphen into year and week, and the year part is returned. The week part is ignored.
ISO year as an integer vector (e.g. 2020).
isoyearweek_to_isoyear_n("2020-10")isoyearweek_to_isoyear_n("2020-10")
Maps an ISO yearweek to a combined ISO year and quarter string of the form "yyyy-Qn".
isoyearweek_to_isoyearquarter_c(x) ## Default S3 method: isoyearweek_to_isoyearquarter_c(x) ## S3 method for class 'character' isoyearweek_to_isoyearquarter_c(x)isoyearweek_to_isoyearquarter_c(x) ## Default S3 method: isoyearweek_to_isoyearquarter_c(x) ## S3 method for class 'character' isoyearweek_to_isoyearquarter_c(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
The output keeps the year part of the input and appends the ISO-week-based
quarter (see isoyearweek_to_isoquarter_c()), for example "2020-Q2".
ISO yearquarter as a character vector (e.g. "2020-Q2").
isoyearweek_to_isoyearquarter_c("2020-19")isoyearweek_to_isoyearquarter_c("2020-19")
Returns the date of the Sunday that ends a given ISO yearweek.
isoyearweek_to_last_date(x) ## Default S3 method: isoyearweek_to_last_date(x) ## S3 method for class 'character' isoyearweek_to_last_date(x)isoyearweek_to_last_date(x) ## Default S3 method: isoyearweek_to_last_date(x) ## S3 method for class 'character' isoyearweek_to_last_date(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2020-19" for the 19th ISO week of 2020. |
ISO weeks run Monday to Sunday, so the returned date is the Sunday of the supplied yearweek.
A base::Date vector giving the Sunday of each ISO yearweek.
isoyearweek_to_last_date(c("2019-19", "2020-01"))isoyearweek_to_last_date(c("2019-19", "2020-01"))
Maps an ISO yearweek to the surveillance season it belongs to, written as "yyyy/yyyy".
isoyearweek_to_season_c(x) ## Default S3 method: isoyearweek_to_season_c(x) ## S3 method for class 'character' isoyearweek_to_season_c(x)isoyearweek_to_season_c(x) ## Default S3 method: isoyearweek_to_season_c(x) ## S3 method for class 'character' isoyearweek_to_season_c(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2021-01". |
Seasons start at ISO week 35 (season week 1). ISO weeks 35 and later belong to the season beginning in that calendar year, while earlier weeks belong to the season that began the previous calendar year. For example "2021-01" falls in season "2020/2021" and "2021-50" falls in season "2021/2022".
Season as a character vector (e.g. "2020/2021").
isoyearweek_to_season_c(c("2021-01", "2021-50"))isoyearweek_to_season_c(c("2021-01", "2021-50"))
Maps an ISO yearweek to its position within the surveillance season, where season week 1 corresponds to ISO week 35.
isoyearweek_to_seasonweek_n(x)isoyearweek_to_seasonweek_n(x)
x |
ISO yearweek as a character string of the form "yyyy-ww", e.g. "2021-01". |
The ISO week is extracted from the yearweek string and then converted with
isoweek_to_seasonweek_n(), so the same season-week numbering and the 18.5
half-step for ISO week 53 apply.
Season week as a numeric vector.
isoyearweek_to_seasonweek_n(c("2021-01", "2021-35"))isoyearweek_to_seasonweek_n(c("2021-01", "2021-35"))
keep_sundays
keep_sundays(dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE)keep_sundays(dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE)
dates |
dates (later) |
format |
format (later) |
keep_delete |
keep_delete (later) |
Label: delete or not
keep_sundays_and_latest_date
keep_sundays_and_latest_date( dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE )keep_sundays_and_latest_date( dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE )
dates |
dates (later) |
format |
format (later) |
keep_delete |
keep_delete (later) |
Date with level: delete or not
keep_sundays_and_latest_date_internal
keep_sundays_and_latest_date_internal( dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE, keep_latest_date = TRUE )keep_sundays_and_latest_date_internal( dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE, keep_latest_date = TRUE )
dates |
dates (later) |
format |
format (later) |
keep_delete |
keep_delet (later) |
keep_latest_date |
keep_latest_date (later) |
A daily calendar flagging Norwegian public holidays, weekends and workdays for each date in the covered range.
nor_workdays_by_datenor_workdays_by_date
A data.table::data.table with one row per date and the following columns:
Date.
Integer. 1 = Monday, 7 = Sunday.
Integer. 1 if Monday to Friday, 0 if Saturday or Sunday.
Integer. 1 if Saturday or Sunday, 0 if Monday to Friday.
Integer. 1 if a public holiday (helligdag), 0 otherwise.
Integer. 1 if a public holiday or Saturday/Sunday, 0 otherwise.
Integer. 1 if freeday is 0, 0 if freeday is 1.
Derived from the Norwegian public-holiday list in
inst/rawdata/norway_holidays.xlsx. See data-raw/1_gen-data.R in the
package source.
head(nor_workdays_by_date) # Count the workdays in the dataset sum(nor_workdays_by_date$workday)head(nor_workdays_by_date) # Count the workdays in the dataset sum(nor_workdays_by_date$workday)
For each complete (7-day) ISO yearweek, the proportion of days that are public holidays, free days and workdays in Norway.
nor_workdays_by_isoyearweeknor_workdays_by_isoyearweek
A data.table::data.table with one row per ISO yearweek and the following columns:
Character. ISO yearweek, "yyyy-ww".
The proportion of days in the ISO yearweek that are public holidays.
The proportion of days in the ISO yearweek that are public holidays or Saturday/Sunday.
1 minus freeday.
Aggregated from nor_workdays_by_date. See data-raw/1_gen-data.R
in the package source.
head(nor_workdays_by_isoyearweek) # ISO yearweeks that contain at least one public holiday head(nor_workdays_by_isoyearweek[nor_workdays_by_isoyearweek$public_holiday > 0, ])head(nor_workdays_by_isoyearweek) # ISO yearweeks that contain at least one public holiday head(nor_workdays_by_isoyearweek[nor_workdays_by_isoyearweek$public_holiday > 0, ])
Returns the current system time formatted as a character string.
now_c(format = "%Y-%m-%d %H:%M:%S")now_c(format = "%Y-%m-%d %H:%M:%S")
format |
A format string passed to |
The current time is taken from base::Sys.time() and formatted with the
supplied format string, which uses the conversion codes documented in
base::strptime().
The current time as a single character string.
now_c() now_c(format = "%Y-%m-%d")now_c() now_c(format = "%Y-%m-%d")
Returns the date of the Sunday that ends a given season.
season_to_last_date(x) ## Default S3 method: season_to_last_date(x) ## S3 method for class 'character' season_to_last_date(x)season_to_last_date(x) ## Default S3 method: season_to_last_date(x) ## S3 method for class 'character' season_to_last_date(x)
x |
Season as a character string of the form "yyyy/yyyy", e.g. "2019/2020". |
A season is written "yyyy/yyyy" where the two years are consecutive (for example "2019/2020"). Seasons are aligned to ISO weeks, with season week 1 starting at ISO week 35; the season therefore ends in late summer of the second year. The returned date is the Sunday of the final week of the season.
A base::Date vector giving the last Sunday of each season.
season_to_last_date(c("2019/2020", "2020/2021"))season_to_last_date(c("2019/2020", "2020/2021"))
Maps a season week number back to its ISO week, returned as a zero-padded
character string. This is the inverse of isoweek_to_seasonweek_n().
seasonweek_to_isoweek_c(x) ## Default S3 method: seasonweek_to_isoweek_c(x) ## S3 method for class 'numeric' seasonweek_to_isoweek_c(x)seasonweek_to_isoweek_c(x) ## Default S3 method: seasonweek_to_isoweek_c(x) ## S3 method for class 'numeric' seasonweek_to_isoweek_c(x)
x |
Season week as a number between 1 and 52. |
Season week 1 corresponds to ISO week 35, season week 2 to ISO week 36, and so on, wrapping around the new year. The ISO week is returned as two digits, e.g. "35" or "01".
ISO week as a character vector (e.g. "35").
seasonweek_to_isoweek_c(1) seasonweek_to_isoweek_c(c(31, 52))seasonweek_to_isoweek_c(1) seasonweek_to_isoweek_c(c(31, 52))
Maps a season week number back to its ISO week, returned as a number. This is
the inverse of isoweek_to_seasonweek_n().
seasonweek_to_isoweek_n(x) ## Default S3 method: seasonweek_to_isoweek_n(x) ## S3 method for class 'numeric' seasonweek_to_isoweek_n(x)seasonweek_to_isoweek_n(x) ## Default S3 method: seasonweek_to_isoweek_n(x) ## S3 method for class 'numeric' seasonweek_to_isoweek_n(x)
x |
Season week as a number between 1 and 52. |
Season week 1 corresponds to ISO week 35, season week 2 to ISO week 36, and so on, wrapping around the new year.
ISO week as an integer vector (1 to 53).
seasonweek_to_isoweek_n(1) seasonweek_to_isoweek_n(c(31, 52))seasonweek_to_isoweek_n(1) seasonweek_to_isoweek_n(c(31, 52))