Fill missing R, U, and V matrices
replace_null_RUV.Rd
In some cases (e.g., bunkers where Last.stage
is "final"),
R, U, U_feed, U_EIOU, or V matrices can be missing, because
imports which appear in the V matrix (or R matrix) are consumed in final demand (Y) matrix,
without any intermediate processing.
When a data frame is pivoted wider by matrices,
the R, U_feed, and U_EIOU columns will contain NULL
entries.
This function fills those NULL
entries with reasonable defaults.
Usage
replace_null_RUV(
.sutmats = NULL,
R = IEATools::psut_cols$R,
U_feed = IEATools::psut_cols$U_feed,
U_eiou = IEATools::psut_cols$U_eiou,
U = IEATools::psut_cols$U,
r_eiou = IEATools::psut_cols$r_eiou,
Y = IEATools::psut_cols$Y,
V = IEATools::psut_cols$V,
resources = IEATools::tpes_flows$resources,
.R_temp_name = ".R_temp",
.U_temp_name = ".U_temp",
.U_feed_temp_name = ".U_feed_temp",
.U_eiou_temp_name = ".U_EIOU_temp",
.r_eiou_temp_name = ".r_EIOU_temp",
.V_temp_name = ".V_temp",
R_name = IEATools::psut_cols$R,
U_name = IEATools::psut_cols$U,
U_feed_name = IEATools::psut_cols$U_feed,
U_eiou_name = IEATools::psut_cols$U_eiou,
r_eiou_name = IEATools::psut_cols$r_eiou,
V_name = IEATools::psut_cols$V
)
Arguments
- .sutmats
A data frame of metadata columns and matrix name columns
- R, U_feed, U_eiou, U, r_eiou, Y, V
See
IEATools::psutcols
. Default values are names for variables incoming with.sutmats
. Can be overridden with actual matrices.- resources
See
IEATools::tpes_flows
. The name of the only row of the output 0 R matrix.- .R_temp_name, .U_temp_name, .U_feed_temp_name, .U_eiou_temp_name, .r_eiou_temp_name, .V_temp_name
Names of temporary variables unused internally to the function.
- R_name, U_name, U_feed_name, U_eiou_name, r_eiou_name, V_name
See
IEATools::psutcols
. The final names for matrices in the output.
Value
A version of .sutmats
with R, U, U_feed, U_EIOU, or V filled with 0 matrices if they were missing.
Details
Reasonable defaults arise from the following thought processes. If all energy is supplied by imports (in the V matrix), there are no resources. Thus, we can replace the missing R matrix with a 0 matrix with a generic "Natural resources" row and the same products as the rows of the Y matrix.
Similarly, missing values for U, U_feed, U_EIOU, or r_EIOU can be replaced by a 0 matrix with row and column names same as a transposed V matrix when it exists. If neither U nor V exist, the R matrix can supply row and column names.
Examples
# Set up a PSUT data frame with NULL for
# R, U_feed, and U_EIOU in 1971 for GHA.
psut <- load_tidy_iea_df() %>%
specify_all() %>%
prep_psut() %>%
tidyr::pivot_longer(cols = c("R", "U_EIOU", "U_feed", "U", "r_EIOU", "V", "Y", "S_units"),
names_to = "matnames", values_to = "matvals") %>%
dplyr::filter(!(Country == "GHA" & Year == 1971 & matnames == "R")) %>%
dplyr::filter(!(Country == "GHA" & Year == 1971 & matnames == "U_feed")) %>%
dplyr::filter(!(Country == "GHA" & Year == 1971 & matnames == "U_EIOU")) %>%
tidyr::pivot_wider(names_from = "matnames", values_from = "matvals")
# Replace the `NULL` matrices in the first row.
res <- psut %>%
replace_null_RUV()
res$R[[1]]
#> Aviation gasoline Charcoal Crude oil Electricity Fuel oil
#> Resources 0 0 0 0 0
#> Gas/diesel oil excl. biofuels Kerosene type jet fuel excl. biofuels
#> Resources 0 0
#> Liquefied petroleum gases (LPG) Lubricants
#> Resources 0 0
#> Motor gasoline excl. biofuels Other kerosene Primary solid biofuels
#> Resources 0 0 0
#> attr(,"rowtype")
#> [1] "Industry"
#> attr(,"coltype")
#> [1] "Product"
res$U_feed[[1]]
#> Charcoal production plants
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Main activity producer electricity plants
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Hydro]
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Primary solid biofuels]
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Oil refineries
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> attr(,"rowtype")
#> [1] "Product"
#> attr(,"coltype")
#> [1] "Industry"
res$U_EIOU[[1]]
#> Charcoal production plants
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Main activity producer electricity plants
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Hydro]
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Primary solid biofuels]
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Oil refineries
#> Charcoal 0
#> Electricity 0
#> Fuel oil 0
#> Gas/diesel oil excl. biofuels 0
#> Hydro 0
#> Kerosene type jet fuel excl. biofuels 0
#> Liquefied petroleum gases (LPG) 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> attr(,"rowtype")
#> [1] "Product"
#> attr(,"coltype")
#> [1] "Industry"