Aggregate a country-level value to a coarser grouping, optionally with population-weighted means.
Arguments
- data
A country-level data frame.
- value
The value column to aggregate (unquoted).
- by
Grouping column(s) (character), default
"region". Combine with"year"for panel roll-ups.- fun
Aggregation:
"sum"(default),"mean","median","min","max"or"weighted_mean".- weight
Optional weight column (unquoted) for
"weighted_mean".
Examples
df <- data.frame(iso3c = c("USA", "CAN", "BRA"),
region = c("North America", "North America", "Latin America"),
gdp = c(21, 1.7, 1.4))
aggregate_regions(df, gdp, fun = "sum")
#> # A tibble: 2 × 2
#> region gdp
#> <chr> <dbl>
#> 1 Latin America 1.4
#> 2 North America 22.7
