Fill NULL R and U matrices
replace_null_UR.RdIn some cases (bunkers where Last.stage is "final"),
R, U_feed, and U_EIOU matrices can be missing, because
imports which appear in the V 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_UR(
.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_feed_temp_name = ".U_feed_temp",
.U_eiou_temp_name = ".U_eEIOU_temp",
.U_temp_name = ".U_temp",
.r_eiou_temp_name = ".r_EIOU_temp",
R_name = IEATools::psut_cols$R,
U_feed_name = IEATools::psut_cols$U_feed,
U_eiou_name = IEATools::psut_cols$U_eiou,
U_name = IEATools::psut_cols$U,
r_eiou_name = IEATools::psut_cols$r_eiou
)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 output0Rmatrix.- .R_temp_name, .U_feed_temp_name, .U_eiou_temp_name, .U_temp_name, .r_eiou_temp_name
Names of temporary variables unused internally to the function.
- R_name, U_feed_name, U_eiou_name, U_name, r_eiou_name
See
IEATools::psutcols. The final names for matrices in the output.
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 NULL R matrix with an 0 matrix with a generic
"Natural resources" row and the same products as the rows of the Y matrix.
Similarly, NULL values for U_feed or U_EIOU can be replaced by a 0 matrix
with row and column names same as a transposed V matrix.
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_UR()
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
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Aviation gasoline]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Crude oil]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Kerosene type jet fuel excl. biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Lubricants]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Other kerosene]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Main activity producer electricity plants
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Hydro]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Primary solid biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Oil refineries Statistical differences
#> Aviation gasoline 0 0
#> Charcoal 0 0
#> Crude oil 0 0
#> Electricity 0 0
#> Fuel oil 0 0
#> Gas/diesel oil excl. biofuels 0 0
#> Hydro 0 0
#> Kerosene type jet fuel excl. biofuels 0 0
#> Liquefied petroleum gases (LPG) 0 0
#> Lubricants 0 0
#> Motor gasoline excl. biofuels 0 0
#> Other kerosene 0 0
#> Primary solid biofuels 0 0
#> Refinery gas 0 0
#> Stock changes [of Gas/diesel oil excl. biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 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
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Aviation gasoline]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Crude oil]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Kerosene type jet fuel excl. biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Lubricants]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Imports [of Other kerosene]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Main activity producer electricity plants
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Hydro]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Manufacture [of Primary solid biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> Oil refineries Statistical differences
#> Aviation gasoline 0 0
#> Charcoal 0 0
#> Crude oil 0 0
#> Electricity 0 0
#> Fuel oil 0 0
#> Gas/diesel oil excl. biofuels 0 0
#> Hydro 0 0
#> Kerosene type jet fuel excl. biofuels 0 0
#> Liquefied petroleum gases (LPG) 0 0
#> Lubricants 0 0
#> Motor gasoline excl. biofuels 0 0
#> Other kerosene 0 0
#> Primary solid biofuels 0 0
#> Refinery gas 0 0
#> Stock changes [of Gas/diesel oil excl. biofuels]
#> Aviation gasoline 0
#> Charcoal 0
#> Crude oil 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
#> Lubricants 0
#> Motor gasoline excl. biofuels 0
#> Other kerosene 0
#> Primary solid biofuels 0
#> Refinery gas 0
#> attr(,"rowtype")
#> [1] "Product"
#> attr(,"coltype")
#> [1] "Industry"