Skip to contents

Given a time series of new cases over a certain date range, compute a windowed percent change value of cases (i.e. one week versus previous 7 days, etc.).

The window is right-aligned by the date column (beginning the most recent date shifting backwards).

Note that because we're computing based on index rather than calendar time, results will be erroneous if data are not complete for every date.

For type == "cases", data should contain at least "date" and "new_cases" columns. For type == "deaths", data should contain at least "date" and "new_deaths" columns.

Usage

calc_window_pct_change(
  data,
  type = c("cases", "deaths"),
  window = 14L,
  return_totals = FALSE
)

Arguments

data

a data.frame with required columns to compute the metric

type

(character) one of cases or deaths, specifying the appropriate basis for the metric

window

(numeric, default: 14) a numeric representing days to calculate the metric over

return_totals

(default: FALSE) return running sums used to compute pct_change?

Value

a df summarized by date with new column pct_change, or pct_change, cases_current, cases_prev if return_totals is TRUE

Examples

if (FALSE) {
data <- get_covid_df()
calc_window_pct_change(window = 14)
# For grouped operations, group data beforehand and pipe:
data |>
  group_by(iso2code, country) |>
  calc_window_pct_change(window = 14)

}