convert_date2fluseason | R Documentation |
Convert a vector of input dates to flu season details. Output on week, month, year, and season are output by default.
convert_date2fluseason(
date,
format = "%Y-%m-%d",
flu_wk_start = 35,
return_values,
split_wk53 = TRUE,
sunday_start = TRUE
)
date |
Character vector in date format of |
format |
Character vector following |
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 |
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.
List containing vectors of week, month, year, and season related to provided dates.
Adapted from original with courtesy of M. Ware.
convert_wk_flu2calendar
, convert_wk_calendar2flu
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.