convert_date2fluseason: Convert calendar date to flu season

View source: R/convert.R

convert_date2fluseasonR Documentation

Convert calendar date to flu season

Description

Convert a vector of input dates to flu season details. Output on week, month, year, and season are output by default.

Usage

convert_date2fluseason(
  date,
  format = "%Y-%m-%d",
  flu_wk_start = 35,
  return_values,
  split_wk53 = TRUE,
  sunday_start = TRUE
)

Arguments

date

Character vector in date format of 'YYYY-mm-dd'.

format

Character vector following strptime; defaults to "%Y-%m-%d".

flu_wk_start

Week of the year that flu season begins, all entries prior to that week will be in prior season; default set to 35.

return_values

Character vector of which values to return, default is set to all ('week', 'month', 'year', 'season').

split_wk53

Boolean value to determine if week 53 values are split based upon which month (Dec or Jan) the day lands.

sunday_start

Boolean value to determine if the start of a week is a Sunday. If set to FALSE, Monday is the start of the week.

Details

The primary purpose of this function is to reassign week 53 dates into week 1 and week 52. It will also provide flu season assignment based upon a flu week start date as reference. To improve speed, indexing methods are used to assign values instead of ifelse (increased speed by over 5-fold). If one needs to convert year-weeks to a full date, it is recommended to use the ISOweek package (ISOweek2date). By default, the week split in this function uses ISO standards.

Value

List containing vectors of week, month, year, and season related to provided dates.

Note

Adapted from original with courtesy of M. Ware.

See Also

convert_wk_flu2calendar, convert_wk_calendar2flu

Examples


# Basic examples
date_list <- c('2022-01-01', '2021-01-01', '2020-08-30', '2020-09-01', '2020-09-23', '2020-01-01', '2019-12-31', '2018-01-01', '2017-01-01', '2016-01-01')
convert_date2fluseason(date_list)
convert_date2fluseason(date_list, return_values = 'season')
convert_date2fluseason(date_list, flu_wk_start = 40)

# Detailed example
library(dplyr)
library(tidyr)

# Create test data for known cases by season
test_data <- data.frame(season = c(1, 1, 1, 2, 2),
                        date = lubridate::ymd(c('2020-06-14', '2020-08-09', '2020-08-16', '2021-08-29', '2021-09-12', '2021-01-01')),
                        n = c(1,1,1,1,2))

# Determine the season with the specific date2fluseason function
test_data$wk <- AHRtools::convert_date2fluseason(test_data$date, return_values = 'week')$week
test_data$yr <- AHRtools::convert_date2fluseason(test_data$date, return_values = 'year')$year

# To fill empty periods join to full combination of season, year, and week
test_data <- full_join(test_data, expand(test_data, season, yr, wk = 1:52), by = c('season', 'yr', 'wk'))
test_data$n <- ifelse(is.na(test_data$n), 0 , test_data$n)
test_data <-  arrange(test_data, season, wk)
test_data$date <- if_else(is.na(test_data$date),
                          ISOweek::ISOweek2date(paste(test_data$yr, paste0('W',test_data$wk), 1, sep = '-')),
                          test_data$date)


al-obrien/farrago documentation built on April 14, 2023, 6:20 p.m.