Skip to content

Commit

Permalink
add tsibble and monday utils
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenturner committed Dec 20, 2021
1 parent a8c7664 commit 0e4635e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ Imports:
dplyr,
lubridate,
magrittr,
MMWRweek,
purrr,
RSocrata,
tibble,
tidyr
tidyr,
tsibble
Depends:
R (>= 2.10)
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export(get_cdc_hosp)
export(get_cdc_ili)
export(get_cdc_vax)
export(get_hdgov_hosp)
export(is_monday)
export(make_tsibble)
export(this_monday)
importFrom(magrittr,"%>%")
2 changes: 2 additions & 0 deletions R/fiphde.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ if(getRversion() >= "2.15.1") utils::globalVariables(c(".",
"rate",
"weeklyrate",
"sea_label",
"monday",
"yweek",
"."))

57 changes: 57 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Make `tsibble`
#'
#' @description
#'
#' This function converts an input `tibble` with columns for \link[lubridate]{epiyear} and \link[lubridate]{epiweek} into a \link[tsibble]{tsibble} object. The `tsibble` has columns specifying indices for the time series as well as a date for the Monday of the epiyear/epiweek combination at each row. Users can optionally ignore the current week when generating the `tsibble` via the "chop" argument.
#'
#' @param df A `tibble` containing columns `epiyear` and `epiweek`.
#' @param epiyear Unquoted variable name containing the MMWR epiyear.
#' @param epiweek Unquoted variable name containing the MMWR epiweek.
#' @param chop Logical indicating whether or not to remove the most current week (default `TRUE`).
#' @return A `tsibble` containing additional columns `monday` indicating the date
#' for the Monday of that epiweek, and `yweek` (a yearweek vctr class object)
#' that indexes the `tsibble` in 1 week increments.
#' @export
#' @md
make_tsibble <- function(df, epiyear, epiweek, chop=TRUE) {
out <- df %>%
# get the monday that starts the MMWRweek
dplyr::mutate(monday=MMWRweek::MMWRweek2Date(MMWRyear={{epiyear}},
MMWRweek={{epiweek}},
MMWRday=2),
.after={{epiweek}}) %>%
# convert represent as yearweek (see package?tsibble)
dplyr::mutate(yweek=tsibble::yearweek(monday), .after="monday") %>%
# convert to tsibble
tsibble::as_tsibble(index=yweek, key=location)
# Remove the incomplete week
if (chop) out <- utils::head(out, -1)
return(out)
}

#' Get Monday
#'
#' @description
#'
#' This function is a helper to get the date for the Monday of the current week.
#'
#' @return Date for the Monday of the current week. For more details see \link[lubridate]{floor_date}.
#' @export
#' @md
#'
this_monday <- function() {
lubridate::floor_date(lubridate::today(), "weeks", week_start = 1)
}

#' Check Monday
#'
#' @description
#'
#' This is a helper function to see if today is Monday.
#
#' @return Logical indicating whether or not today is Monday
#' @export
#' @md
is_monday <- function() {
lubridate::wday(lubridate::today(), label=TRUE) %in% c("Mon")
}
14 changes: 14 additions & 0 deletions man/is_monday.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/make_tsibble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/this_monday.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e4635e

Please sign in to comment.