Calculate balances on a tidy IEA data frame
calc_tidy_iea_df_balances.RdIt is important to know whether energy flows are balanced before
proceeding with further analyses.
This function calculates energy balances
by groups in .tidy_iea_df.
So be sure to group .tidy_iea_df by appropriate variables
before calling this function.
Grouping should definitely be done on the Product column.
Typically, grouping is also done on
Country, Method, Year, EnergyType, Last.stage, etc. columns.
Grouping should not be done on the Ledger.side column or the Flow column.
To test whether all balances are OK,
use the tidy_iea_df_balanced() function.
Usage
calc_tidy_iea_df_balances(
.tidy_iea_df,
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,
balancing = "balancing",
supply_sum = "supply_sum",
consumption_sum = "consumption_sum",
supply_minus_consumption = "supply_minus_consumption",
balance_OK = "balance_OK",
err = "err",
tol = 1e-06
)Arguments
- .tidy_iea_df
an IEA-style data frame containing a
ledger_side,product,flow, and energy rate (e_dot) columns along with grouping columns, typicallyCountry,Year,Product, etc. aLedger.sidecolumn.- ledger_side, flow_aggregation_point, flow, product, e_dot, unit
See
IEATools::iea_cols.- supply, consumption
- matnames
- balancing
The ledger side of balancing flows, if any balancing flow has been added to the
.tidy_iea_df.- supply_sum
the name of a new column that will contain the sum of all supply for that group. Default is "supply_sum".
- consumption_sum
the name of a new column that will contain the sum of all consumption for that group. Default is "consumption_sum".
- supply_minus_consumption
the name of a new column that will contain the difference between supply and consumption for that group. Default is "supply_minus_consumption".
- balance_OK
the name of a new logical column that tells whether a row's energy balance is OK. Default is "balance_OK".
- err
the name of a new column that indicates the energy balance error for each group. Default is "err".
- tol
if the difference between supply and consumption is greater than
tol,balance_OKwill be set toFALSE. Default is1e-6.
Value
.tidy_iea_df with additional columns supply_sum, consumption_sum, supply_minus_consumption, balance_OK, and err.
Details
Supply side and consumption side energy flows are aggregated to a
supply_sum and a consumption_sum column.
There are two possibilities:
A Product appears only on the supply side, because it is completely transformed before reaching the consumption side of the ledger. In this case, the
consumption_sumcolumn will have anNAvalue, and thesupply_minus_consumptioncolumn will also have anNAvalue.A Product appears on both the supply and the demand sides of the ledger and, therefore, is not
NAin theconsumption_sumcolumn and thesupply_minus_consumptioncolumn. The columnbalance_OKis calculated as follows:For the first situation,
consumption_sumwill be0(withintol) if the Product is balanced andbalance_OKwill have a value ofTRUE. If not,balance_OKwill have a value ofFALSE.In the second situation, the difference between
supply_sumandconsumption_sumis calculated (supply_minus_consumption). If the product is balanced,supply_minus_consumptionwill be0(withintol) andbalance_OKwill beTRUE. If not,balance_OKwill beFALSE.
Examples
library(dplyr)
Ebal <- load_tidy_iea_df() %>%
calc_tidy_iea_df_balances()
head(Ebal, 5)
#> # A tibble: 5 × 12
#> Country Method EnergyType LastStage Year Product Unit supply_sum
#> <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl>
#> 1 GHA PCM E Final 1971 Aviation gasoline TJ 0
#> 2 GHA PCM E Final 1971 Charcoal TJ 4990.
#> 3 GHA PCM E Final 1971 Crude oil TJ -0.000100
#> 4 GHA PCM E Final 1971 Electricity TJ 9846.
#> 5 GHA PCM E Final 1971 Fuel oil TJ 3980.
#> # ℹ 4 more variables: consumption_sum <dbl>, supply_minus_consumption <dbl>,
#> # balance_OK <lgl>, err <dbl>