rollSummaryXDays: Create Rolling Summaries Given a Number of Days

View source: R/rollSummaryXDays.R

rollSummaryXDaysR Documentation

Create Rolling Summaries Given a Number of Days

Description

Given a formatted variable table, this function calculates the rolling averages and totals for the specified columns over a specified number of days.

Usage

rollSummaryXDays(df, numDays = 7, varTable)

Arguments

df

Input data.frame to be summarized

numDays

The number of days to do the summary over. Defaults at 7 days.

varTable

A formatted data.frame containing variable, Avg, Total, CalcString columns. See details for more information.

Details

The format of varTable:

variable

Variable names that you want to create summaries for

Avg

TRUE/FALSE on whether X day averages should be caclulated for the given variable

Total

TRUE/FALSE on whether X day totals should be caclulated for the given variable

CalcString

Used for pre-summary mutations to create the variables needed; see mutateCalcString. If none are needed, supply a vector of NA's

Examples

## Not run: 

# Load in CDT and Hospital Base Data
cdtData <- downloadBaseData(type = "CDT")
hospData <-  downloadBaseData(type = "Hospital")


# Combine Hospital and CDT Base Data
cdtHospData <- dplyr::full_join(cdtData, hospData, by = c("GeoID", "Date")) %>%
    marcR::coalesceJoin() %>%
    dplyr::mutate(CovidNew = CovidNew24HConfirmed + CovidNew24HSuspected) %>%
    dplyr::select(
        Jurisdiction, State, GeoID, Region, Date,

        CasesNew, CasesTotal,
        DeathsNew, DeathsTotal,
        TestsNew, TestsTotal,
        Population,

        HospitalsReporting, HospitalsTotal,
        BedsTotal, BedsUsed, BedsAvailable,
        BedsICUTotal, BedsICUUsed, BedsICUAvailable,
        CovidTotal,
        CovidNew,
        CovidICUTotal, CovidICUConfirmed, CovidICUSuspected,
        VentilatorsTotal, VentilatorsUsed, VentilatorsAvailable,
        CovidVentilatorsUsed)

# Calculate Rolling Average Tables
varTable <- tibble::tribble(
    ~variable,                  ~Avg,         ~Total,     ~CalcString,
    "CasesNew",                 TRUE,         TRUE,       NA,
    "DeathsNew",                TRUE,         TRUE,       NA,
    "TestsNew",                 TRUE,         TRUE,       NA,
    "CovidNew",                 TRUE,         TRUE,       NA,
    "CovidTotal",               TRUE,         FALSE,      NA,
    "BedsUsedOther",            TRUE,         FALSE,      "BedsUsed - CovidTotal",
    "BedsAvailable",            TRUE,         FALSE,      NA,
    "CovidICUTotal",            TRUE,         FALSE,      NA,
    "BedsICUUsedOther",         TRUE,         FALSE,      "BedsICUUsed - CovidICUTotal",
    "BedsICUAvailable",         TRUE,         FALSE,      NA,
    "CovidVentilatorsUsed",     TRUE,         FALSE,      NA,
    "VentilatorsUsedOther",     TRUE,         FALSE,      "VentilatorsUsed - CovidVentilatorsUsed",
    "VentilatorsAvailable",     TRUE,         FALSE,      NA,
    "HospitalsReporting",       TRUE,         TRUE,       NA,
    "HospitalsTotal",           TRUE,         TRUE,       NA
)


cdtHosp7DayRollingData <- rollSummaryXDays(df = cdtHospData, numDays = 7, varTable = varTable)


## End(Not run)



MARC-KC/Covid19MARCData documentation built on April 7, 2022, 11:43 a.m.