Calculate balances on a tidy IEA data frame
calc_tidy_iea_df_balances.Rd
It 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
, Energy.type
, 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.side
column.- 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_OK
will 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_sum
column will have anNA
value, and thesupply_minus_consumption
column will also have anNA
value.A Product appears on both the supply and the demand sides of the ledger and, therefore, is not
NA
in theconsumption_sum
column and thesupply_minus_consumption
column. The columnbalance_OK
is calculated as follows:For the first situation,
consumption_sum
will be0
(withintol
) if the Product is balanced andbalance_OK
will have a value ofTRUE
. If not,balance_OK
will have a value ofFALSE
.In the second situation, the difference between
supply_sum
andconsumption_sum
is calculated (supply_minus_consumption
). If the product is balanced,supply_minus_consumption
will be0
(withintol
) andbalance_OK
will beTRUE
. If not,balance_OK
will beFALSE
.
Examples
library(dplyr)
Ebal <- load_tidy_iea_df() %>%
calc_tidy_iea_df_balances()
head(Ebal, 5)
#> # A tibble: 5 × 12
#> Country Method Energy.type Last.stage 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 4.99e+3
#> 3 GHA PCM E Final 1971 Crude oil TJ -1.00e-4
#> 4 GHA PCM E Final 1971 Electricity TJ 9.85e+3
#> 5 GHA PCM E Final 1971 Fuel oil TJ 3.98e+3
#> # ℹ 4 more variables: consumption_sum <dbl>, supply_minus_consumption <dbl>,
#> # balance_OK <lgl>, err <dbl>