region_aggregates | R Documentation |
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.
region_aggregates(
.sut_data,
many_colname = IEATools::iea_cols$country,
few_colname = Recca::aggregate_cols$region,
drop_na_few = FALSE,
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, Y_fu_details = Recca::psut_cols$Y_fu_details,
U_eiou_fu_details = Recca::psut_cols$U_eiou_fu_details),
matrix_names = Recca::psut_cols$matnames,
matrix_values = Recca::psut_cols$matvals
)
.sut_data |
A wide-by-matrices |
many_colname |
The name of the column in |
few_colname |
The of the column in |
drop_na_few |
A boolean that tells whether to ignore (not aggregate) rows with |
year, method, energy_type, last_stage |
See |
matrix_cols |
Names of columns in .sut_data containing matrices.
Default is a vector of names from |
matrix_names, matrix_values |
Internal column names. See |
The argument drop_na_few
controls what happens when an item many_colname
does not have a corresponding value in few_colname
.
This condition can occur when, say, "WRLD" is a country.
"WRLD" (as a country in many_colname
)
should not be aggregated to "World" (as a region in the few_colname
).
In those circumstances,
a well-formed aggregation_map
will leave NA
in few_colname
.
Setting drop_na_few
to TRUE
(default is FALSE
)
will eliminate rows with NA
in few_colname
before doing the aggregation so those NA
rows do not end up as
NA
in the outgoing data frame.
The default value for drop_na_few
is FALSE
,
because setting to TRUE
will result in data loss.
You need to opt in to this behavior when you know it's what you want.
If all of few_colname
entries are NA
and
drop_na_few
is TRUE
,
a zero-row data frame of the same structure as .sut_data
is returned.
This function works for both a data frame of PSUT matrices and a data frame of details matrices.
An aggregated version of .sut_data
wherein the many_colname
column is replaced
by few_colname
as specified by aggregation_map
.
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(many_colname = "Country", few_colname = "Continent")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.