Skip to contents

Aggregates a data frame according to the regions given in a column of the data frame. The data frame (.sut_data) should contain metadata columns (including many_colname and few_colname) and be wide-by-matrices.

Usage

region_aggregates(
  .sut_data,
  many_colname = IEATools::iea_cols$country,
  few_colname = Recca::aggregate_cols$region,
  year = IEATools::iea_cols$year,
  method = IEATools::iea_cols$method,
  energy_type = IEATools::iea_cols$energy_type,
  last_stage = IEATools::iea_cols$last_stage,
  matrix_cols = c(R = Recca::psut_cols$R, U = Recca::psut_cols$U, U_feed =
    Recca::psut_cols$U_feed, U_eiou = Recca::psut_cols$U_eiou, r_eiou =
    Recca::psut_cols$r_eiou, V = Recca::psut_cols$V, Y = Recca::psut_cols$Y, S_units =
    Recca::psut_cols$S_units),
  matrix_names = Recca::psut_cols$matnames,
  matrix_values = Recca::psut_cols$matvals
)

Arguments

.sut_data

A wide-by-matrices matsindf-style data frame of PSUT matrices.

many_colname

The name of the column in .sut_data that contains the "many" descriptions, for example countries that need to be aggregated to continents. Default is IEATools::iea_cols$country.

few_colname

The of the column in .sut_data that contains the "few" descriptions, for example continents into which countries are to be aggregated. Default is Recca::aggregate_cols$region.

year, method, energy_type, last_stage

See IEATools::iea_cols.

matrix_cols

Names of columns in .sut_data containing matrices. Default is a vector of names from Recca::psut_cols: R, U, U_feed, U_eiou, r_eiou, V, Y, and S_units.

matrix_names, matrix_values

Internal column names. See Recca::psut_cols.

Value

An aggregated version of .sut_data wherein the many_colname column is replaced by few_colname as specified by aggregation_map.

Examples

library(dplyr)
library(matsbyname)
library(tidyr)
mats_GBR <- UKEnergy2000mats %>%
  tidyr::pivot_wider(names_from = matrix.name, values_from = matrix)
# Add other countries, by duplicating and renaming GBR
mats <- dplyr::bind_rows(mats_GBR,
                         mats_GBR %>% dplyr::mutate(Country = "USA"),
                         mats_GBR %>% dplyr::mutate(Country = "FRA"))
# Establish the aggregation map.
agg_df <- list(EUR = c("GBR", "FRA"), AMR = "USA") %>%
  matsbyname::agg_map_to_agg_table(few_colname = "Continent", many_colname = "Country")
# Aggregate into continents
dplyr::left_join(mats, agg_df, by = "Country") %>%
  region_aggregates(mats, many_colname = "Country", few_colname = "Continent")
#> # A tibble: 8 × 12
#>    Year Energy.type Last.…¹ Country R        S_units  U_EIOU   U_feed   V       
#>   <dbl> <chr>       <chr>   <chr>   <list>   <list>   <list>   <list>   <list>  
#> 1  2000 E           Final   AMR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 2  2000 E           Final   EUR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 3  2000 E           Servic… AMR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 4  2000 E           Servic… EUR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 5  2000 E           Useful  AMR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 6  2000 E           Useful  EUR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 7  2000 X           Servic… AMR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> 8  2000 X           Servic… EUR     <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]> <dbl[…]>
#> # … with 3 more variables: Y <list>, U <list>, r_EIOU <list>, and abbreviated
#> #   variable name ¹​Last.stage