prep_psut: Prepare for PSUT analysis

View source: R/psut.R

prep_psutR Documentation

Prepare for PSUT analysis

Description

Converts a tidy IEA data frame into a PSUT data frame by collapsing the IEA data into PSUT matrices (R, U, V, Y, and S_units).

Usage

prep_psut(
  .tidy_iea_df,
  matrix_class = c("matrix", "Matrix"),
  year = IEATools::iea_cols$year,
  ledger_side = IEATools::iea_cols$ledger_side,
  flow_aggregation_point = IEATools::iea_cols$flow_aggregation_point,
  flow = IEATools::iea_cols$flow,
  product = IEATools::iea_cols$product,
  e_dot = IEATools::iea_cols$e_dot,
  unit = IEATools::iea_cols$unit,
  supply = IEATools::ledger_sides$supply,
  consumption = IEATools::ledger_sides$consumption,
  matnames = IEATools::mat_meta_cols$matnames,
  rownames = IEATools::mat_meta_cols$rownames,
  colnames = IEATools::mat_meta_cols$colnames,
  rowtypes = IEATools::mat_meta_cols$rowtypes,
  coltypes = IEATools::mat_meta_cols$coltypes,
  matvals = IEATools::psut_cols$matvals,
  R = IEATools::psut_cols$R,
  U_eiou = IEATools::psut_cols$U_eiou,
  U_feed = IEATools::psut_cols$U_feed,
  r_eiou = IEATools::psut_cols$r_eiou,
  U = IEATools::psut_cols$U,
  V = IEATools::psut_cols$V,
  Y = IEATools::psut_cols$Y,
  B = IEATools::psut_cols$B,
  s_units = IEATools::psut_cols$s_units
)

Arguments

.tidy_iea_df

a tidy data frame that has been specified with specify_all().

matrix_class

The type of matrix to be created, one of "matrix" or "Matrix". Default is "matrix".

year, ledger_side, flow_aggregation_point, flow, product, e_dot, unit

See IEATools::iea_cols.

supply, consumption

See IEATools::ledger_sides.

matnames, rownames, colnames, rowtypes, coltypes

See IEATools::mat_meta_cols.

matvals, R, U_eiou, U_feed, U, r_eiou, V, Y, s_units, B

See IEATools::psut_cols.

Details

This function bundles several others:

  1. add_psut_matnames()

  2. add_row_col_meta()

  3. collapse_to_tidy_psut()

  4. replace_null_RUV()

Furthermore, it extracts S_units matrices using extract_S_units_from_tidy() and adds those matrices to the data frame.

If .tidy_iea_df is a zero-row data frame, the return value is a zero-row data frame with expected columns.

Value

A wide-by-matrix data frame with metadata columns and columns named for each type of matrix.

Examples

library(dplyr)
library(tidyr)
Simple <- load_tidy_iea_df() %>% 
  specify_all() %>% 
  prep_psut() %>% 
  pivot_longer(cols = c(R, U_EIOU, U_feed, V, Y, S_units), 
               names_to = "matnames",
               values_to = "matval_simple")
S_units <- load_tidy_iea_df() %>% 
  specify_all() %>% 
  extract_S_units_from_tidy()
Complicated <- load_tidy_iea_df() %>% 
  specify_all() %>% 
  add_psut_matnames() %>% 
  add_row_col_meta() %>% 
  collapse_to_tidy_psut() %>% 
  spread(key = matnames, value = matvals) %>% 
  replace_null_RUV() %>% 
  full_join(S_units, by = c("Method", "Energy.type", "Last.stage", 
                            "Country", "Year")) %>% 
  gather(key = matnames, value = matvals, R, U_EIOU, U_feed, 
                                        V, Y, S_units) %>% 
  rename(matval_complicated = matvals)
# Simple and Complicated are same.
full_join(Simple, Complicated, by = c("Method", "Energy.type", 
                                      "Last.stage", "Country", 
                                      "Year", "matnames")) %>% 
  dplyr::mutate(
    same = matsbyname::equal_byname(matval_simple, matval_complicated)
  ) %>% 
  magrittr::extract2("same") %>% 
  as.logical() %>% 
  all()

MatthewHeun/IEATools documentation built on Feb. 6, 2024, 3:29 p.m.