Skip to contents

The IEA data frame to be sorted can be either (a) tidy (long) where each observation is on its own row and there is a year column present or (b) wide where year columns are spread to the right.

Usage

sort_iea_df(
  .iea_df,
  col_names = IEATools::iea_cols,
  country = col_names$country,
  method = col_names$method,
  energy_type = col_names$energy_type,
  last_stage = col_names$last_stage,
  year = col_names$year,
  ledger_side = col_names$ledger_side,
  flow_aggregation_point = col_names$flow_aggregation_point,
  flow = col_names$flow,
  sep = "_",
  fap_flow = paste0(flow_aggregation_point, sep, flow),
  product = col_names$product,
  country_order = IEATools::countries,
  method_order = IEATools::methods,
  energy_type_order = IEATools::energy_types,
  last_stage_order = IEATools::last_stages,
  ledger_side_iea_order = IEATools::ledger_sides,
  fap_flow_iea_order = IEATools::fap_flows,
  product_iea_order = IEATools::products,
  .clean_flow = ".clean_flow",
  .clean_product = ".clean_product"
)

Arguments

.iea_df

the IEA data frame to be sorted

col_names

a list of column names in IEA data frames. Default is IEATools::iea_cols.

country

the name of the country column in .tidy_iea_df. Default is "Country".

method

the name of the method column in .tidy_iea_df. Default is "Method".

energy_type

the name of the energy type column in .tidy_iea_df. Default is "Energy.type".

last_stage

the name of the last stage column in .tidy_iea_df. Default is "Last.stage".

year

the name of the year column in .tidy_iea_df. Default is "Year".

ledger_side

the name of the ledger side column in .tidy_iea_df. Default is "Ledger.side".

flow_aggregation_point

the name of the flow aggregation point column in .tidy_iea_df. Default is "Flow.aggregation.point".

flow

the name of the flow column in .tidy_iea_df. Default is "Flow".

sep

a separator between the flow aggregation point column and the flow column. Used when uniting those two columns internally. Default is "_".

fap_flow

the name of the united flow aggregation point and flow column to be created internally in .tidy_iea_df. Default is "Flow.aggregation.point_Flow".

product

the name of the product column in .tidy_iea_df. Default is "Product".

country_order

the order in which to sort the country column of .tidy_iea_df. Default is countries.

method_order

the order in which to sort the method column of .tidy_iea_df. Default is methods.

energy_type_order

the order in which to sort the energy_type column of .tidy_iea_df. Default is energy_types.

last_stage_order

the order in which to sort the last_stage column of .tidy_iea_df. Default is last_stages.

ledger_side_iea_order

the order in which to sort the ledger_side column of .tidy_iea_df. Default is ledger_sides.

fap_flow_iea_order

the order in which to sort the united flow_aggregation_point and flow columns of .tidy_iea_df. Default is fap_flows.

product_iea_order

the order in which to sort the product column of .tidy_iea_df. Default is products.

.clean_flow

the name of an internally-generated column in .iea_df that stores a de-specified version of the flow column. Default is ".clean_flow".

.clean_product

the name of an internally-generated column in .iea_df that stores a de-specified version of the product column. Default is ".clean_product".

Value

a version of .tidy_iea_df sorted in IEA order

Details

Sorting is accomplished (by default) using the values of the arguments countries, methods, energy_type, last_stage, year (if present), ledger_side, fap_flows, and product (in that order of precedence).

Years are sorted if the year column is present (a tidy data frame). If years are not present, they are assumed to be spread to the right to create a wide data frame. Wide data frames are sorted in the same order.

Examples

library(dplyr)
tidy <- load_tidy_iea_df()
# See first and last rows
head(tidy)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 2 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> 3 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 4 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> 5 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 6 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>
tail(tidy)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 2 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 3 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 4 ZAF     PCM    E           Final       1971 Consumption Non-energy use        
#> 5 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 6 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>
# Move the first row to the bottom to put everything out of order
unsorted <- tidy[-1, ] %>% 
  bind_rows(tidy[1, ])
head(unsorted)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> 2 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 3 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> 4 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 5 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> 6 GHA     PCM    E           Final       2000 Supply      Total primary energy …
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>
tail(unsorted)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 2 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 3 ZAF     PCM    E           Final       1971 Consumption Non-energy use        
#> 4 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 5 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 6 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>
# Now sort it
sorted <- sort_iea_df(unsorted)
head(sorted)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 2 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 3 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 4 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 5 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> 6 GHA     PCM    E           Final       1971 Supply      Total primary energy …
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>
tail(sorted)
#> # A tibble: 6 × 11
#>   Country Method Energy.type Last.stage  Year Ledger.side Flow.aggregation.point
#>   <chr>   <chr>  <chr>       <chr>      <dbl> <chr>       <chr>                 
#> 1 ZAF     PCM    E           Final       2000 Consumption Other                 
#> 2 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 3 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 4 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 5 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> 6 ZAF     PCM    E           Final       2000 Consumption Non-energy use        
#> # ℹ 4 more variables: Flow <chr>, Product <chr>, Unit <chr>, E.dot <dbl>