aggregate_fun: Aggregating data

Description Usage Arguments Examples

View source: R/Functions.R

Description

Function that takes in daily causes of death and aggregates it by week (WOD) or months (MOD) and by country or by province. The arg_match function ensures the user can choose the only two available options for the time aggregation and returns an informative error message. This functions outputs an aggreated dataframe.

Usage

1
2
3
aggregate_fun(df,
              time.aggregate = c("WOD", "MOD"),
              scale.aggregate = c("Province", "Countrywide"))

Arguments

df

dataframe

time.aggregate

Whether to aggregate weekly ("WOD") or monthly ("MOD")

by_province

Whether to aggregate by province or country level

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## The function is currently defined as

aggregate_fun <- function(df,
                          time.aggregate = c("WOD", "MOD"),
                          scale.aggregate = c("Province", "Countrywide")) {

  time.aggregate = rlang::arg_match(time.aggregate)
  quo.time.aggregate = rlang::sym(time.aggregate)
  scale.aggregate = rlang::arg_match(scale.aggregate)

  group_df <- if (scale.aggregate == "Province")
  {group_by(df, !! quo.time.aggregate, DeathProv)}
  else {group_by(df, !! quo.time.aggregate)}

  group_df %>%
    summarise(All_deaths = sum(All),
              AllRes_deaths = sum(AllRes), AllResU5_deaths = sum(AllResU5),
              AllResG5_deaths = sum(AllResG5), PI_deaths = sum(PI),
              PIU5_deaths = sum(PIU5), PIG5_deaths = sum(PIG5),
              POther_deaths = sum(POther),
              Flu_deaths = sum(Flu), RSV_deaths = sum(RSV)) %>%
    mutate(Year = as.numeric(format(!! quo.time.aggregate, "%Y")))%>%
    ungroup()
}

EMDominic/SThelperfunctions documentation built on Aug. 4, 2020, 12:02 a.m.