Skip to content
Snippets Groups Projects
wrangler_functions_method2_ixi.R 32.59 KiB
# data wrangling functions

## libs
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,
               here,
               vroom,
               ISOcodes)

#### load raw mrio results
read_raw <- function(peu_q_count = 10) {
  dat_raw = vroom(here("analysis", "data", "derived", "si", paste0("mrio_results_eu_ntile_mapped_n_", peu_q_count, "_method2_ixi.csv")))
  dat_raw
}

# get mrio results aggregated by country summarized by country quintile
get_country_summary_by_cquint_and_euntile <- function(peu_q_count = 10) {
  dat_raw = read_raw(peu_q_count)
  dat_summary_country_quintile = dat_raw %>%
    group_by(year, iso2, quint, eu_q_rank) %>%
    summarise(total_fd_me = sum(fd_me, na.rm = T),
              # totals
              total_adult_eq = first(adult_eq),
              total_co2_kg = sum(co2_kg),
              total_co2_dom_kg = sum(co2_dom_kg),
              total_co2_eu_kg = sum(co2_eu_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg,
              total_co2eq_kg = sum(co2eq_kg),
              total_co2eq_dom_kg = sum(co2eq_dom_kg),
              total_co2eq_eu_kg = sum(co2eq_eu_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg,
              total_energy_use_tj = sum(energy_use_tj),
              total_energy_use_dom_tj = sum(energy_use_dom_tj),
              total_energy_use_eu_tj = sum(energy_use_eu_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_summary_country_quintile
}

# get mrio results aggregated by country summarized by eu quintile
get_country_summary_by_eu_ntile <- function(peu_q_count = 10) {
  dat_summary_by_country_quintile = get_country_summary_by_cquint_and_euntile(peu_q_count)
  dat_summary_by_eu_ntile = dat_summary_by_country_quintile %>%
    group_by(year, iso2, eu_q_rank) %>%
    summarise(total_fd_me = sum(total_fd_me),
              # totals
              total_adult_eq = sum(total_adult_eq),
              total_co2_kg = sum(total_co2_kg),
              total_co2_dom_kg = sum(total_co2_dom_kg),
              total_co2_eu_kg = sum(total_co2_eu_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg,
              total_co2eq_kg = sum(total_co2eq_kg),
              total_co2eq_dom_kg = sum(total_co2eq_dom_kg),
              total_co2eq_eu_kg = sum(total_co2eq_eu_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg,
              total_energy_use_tj = sum(total_energy_use_tj),
              total_energy_use_dom_tj = sum(total_energy_use_dom_tj),
              total_energy_use_eu_tj = sum(total_energy_use_eu_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_summary_by_eu_ntile
}




# get mrio results aggregated by five sectors and summarized by country quintile
get_sector_summary_by_country_quintile <- function(peu_q_count = 10) {
  dat_sectors_by_country_quintile = read_raw(peu_q_count) %>%
    group_by(year, iso2, quint, eu_q_rank, coicop) %>%
    summarise(total_fd_me = sum(fd_me),
              # totals
              total_adult_eq = first(adult_eq),
              total_co2_kg = sum(co2_kg),
              total_co2_dom_kg = sum(co2_dom_kg),
              total_co2_eu_kg = sum(co2_eu_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg,
              total_co2eq_kg = sum(co2eq_kg),
              total_co2eq_dom_kg = sum(co2eq_dom_kg),
              total_co2eq_eu_kg = sum(co2eq_eu_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg,
              total_energy_use_tj = sum(energy_use_tj),
              total_energy_use_dom_tj = sum(energy_use_dom_tj),
              total_energy_use_eu_tj = sum(energy_use_eu_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_sectors_by_country_quintile
}



# get mrio results aggregated by five sectors and summarized by country quintile
get_sector_summary_by_country_quintile_direct <- function(peu_q_count = 10) {
  dat_sectors_by_country_quintile = read_raw(peu_q_count) %>%
    ## shift domestic use to new direct column for direct sectors
    #mutate(co2eq_direct_kg = if_else(sector_id %in% c(164,165,166), co2eq_dom_kg, 0),
    #       co2_direct_kg = if_else(sector_id %in% c(164,165,166), co2_dom_kg, 0),
    #       energy_use_direct_tj = if_else(sector_id %in% c(164,165,166), energy_use_dom_tj, 0)) %>%
    #mutate(co2eq_dom_kg = if_else(sector_id %in% c(164,165,166), 0, co2eq_dom_kg),
    #       co2_dom_kg = if_else(sector_id %in% c(164,165,166), 0, co2_dom_kg),
    #       energy_use_dom_tj = if_else(sector_id %in% c(164,165,166), 0, energy_use_dom_tj)) %>%
    group_by(year, iso2, quint, eu_q_rank, coicop) %>%
    summarise(total_fd_me = sum(fd_me),
              # totals
              total_adult_eq = first(adult_eq),
              total_co2_kg = sum(co2_kg),
              total_co2_dom_kg = sum(co2_dom_kg),
              total_co2_eu_kg = sum(co2_eu_kg),
              total_co2_direct_kg = sum(co2_direct_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg - total_co2_direct_kg,
              total_co2eq_kg = sum(co2eq_kg),
              total_co2eq_dom_kg = sum(co2eq_dom_kg),
              total_co2eq_eu_kg = sum(co2eq_eu_kg),
              total_co2eq_direct_kg = sum(co2eq_direct_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg -total_co2eq_direct_kg,
              total_energy_use_tj = sum(energy_use_tj),
              total_energy_use_dom_tj = sum(energy_use_dom_tj),
              total_energy_use_eu_tj = sum(energy_use_eu_tj),
              total_energy_use_direct_tj = sum(energy_use_direct_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj - total_energy_use_direct_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_direct_t = total_co2_direct_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_direct_t = total_co2eq_direct_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_direct_gj = total_energy_use_direct_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_direct_kg = total_co2_direct_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_direct_kg = total_co2eq_direct_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_direct_mj = total_energy_use_direct_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_sectors_by_country_quintile
}


# get mrio results aggregated by five sectors and summarized by eu ntile
get_sector_summary_by_eu_ntile <- function(peu_q_count = 10) {
  dat_sectors_by_country_quintile = get_sector_summary_by_country_quintile(peu_q_count)
  dat_sectors_by_eu_ntile = dat_sectors_by_country_quintile %>%
    group_by(year, eu_q_rank, coicop) %>%
    summarise(total_fd_me = sum(total_fd_me),
              # totals
              total_adult_eq = sum(total_adult_eq),
              total_co2_kg = sum(total_co2_kg),
              total_co2_dom_kg = sum(total_co2_dom_kg),
              total_co2_eu_kg = sum(total_co2_eu_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg,
              total_co2eq_kg = sum(total_co2eq_kg),
              total_co2eq_dom_kg = sum(total_co2eq_dom_kg),
              total_co2eq_eu_kg = sum(total_co2eq_eu_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg,
              total_energy_use_tj = sum(total_energy_use_tj),
              total_energy_use_dom_tj = sum(total_energy_use_dom_tj),
              total_energy_use_eu_tj = sum(total_energy_use_eu_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_sectors_by_eu_ntile
}



# get mrio results aggregated by five sectors and summarized by eu ntile
get_sector_summary_by_eu_ntile_direct <- function(peu_q_count = 10) {
  dat_sectors_by_country_quintile = get_sector_summary_by_country_quintile_direct(peu_q_count)
  dat_sectors_by_eu_ntile = dat_sectors_by_country_quintile %>%
    group_by(year, eu_q_rank, coicop) %>%
    summarise(total_fd_me = sum(total_fd_me),
              # totals
              total_adult_eq = sum(total_adult_eq),
              total_co2_kg = sum(total_co2_kg),
              total_co2_dom_kg = sum(total_co2_dom_kg),
              total_co2_eu_kg = sum(total_co2_eu_kg),
              total_co2_direct_kg = sum(total_co2_direct_kg),
              total_co2_noneu_kg = total_co2_kg - total_co2_dom_kg - total_co2_eu_kg - total_co2_direct_kg,
              total_co2eq_kg = sum(total_co2eq_kg),
              total_co2eq_dom_kg = sum(total_co2eq_dom_kg),
              total_co2eq_eu_kg = sum(total_co2eq_eu_kg),
              total_co2eq_direct_kg = sum(total_co2eq_direct_kg),
              total_co2eq_noneu_kg = total_co2eq_kg - total_co2eq_dom_kg - total_co2eq_eu_kg - total_co2eq_direct_kg,
              total_energy_use_tj = sum(total_energy_use_tj),
              total_energy_use_dom_tj = sum(total_energy_use_dom_tj),
              total_energy_use_eu_tj = sum(total_energy_use_eu_tj),
              total_energy_use_direct_tj = sum(total_energy_use_direct_tj),
              total_energy_use_noneu_tj = total_energy_use_tj - 
                total_energy_use_dom_tj - 
                total_energy_use_eu_tj - total_energy_use_direct_tj,
              #Per adult equivalent values
              pae_fd_ke = total_fd_me*1000000/total_adult_eq*0.001,
              pae_co2_t = total_co2_kg/total_adult_eq*0.001,
              pae_co2_dom_t = total_co2_dom_kg/total_adult_eq*0.001,
              pae_co2_eu_t = total_co2_eu_kg/total_adult_eq*0.001,
              pae_co2_direct_t = total_co2_direct_kg/total_adult_eq*0.001,
              pae_co2_noneu_t = total_co2_noneu_kg/total_adult_eq*0.001,
              pae_co2eq_t = total_co2eq_kg/total_adult_eq*0.001,
              pae_co2eq_dom_t = total_co2eq_dom_kg/total_adult_eq*0.001,
              pae_co2eq_eu_t = total_co2eq_eu_kg/total_adult_eq*0.001,
              pae_co2eq_direct_t = total_co2eq_direct_kg/total_adult_eq*0.001,
              pae_co2eq_noneu_t = total_co2eq_noneu_kg/total_adult_eq*0.001,
              pae_energy_use_gj = total_energy_use_tj*1000/total_adult_eq,
              pae_energy_use_dom_gj = total_energy_use_dom_tj*1000/total_adult_eq,
              pae_energy_use_eu_gj = total_energy_use_eu_tj*1000/total_adult_eq,
              pae_energy_use_direct_gj = total_energy_use_direct_tj*1000/total_adult_eq,
              pae_energy_use_noneu_gj = total_energy_use_noneu_tj*1000/total_adult_eq,
              # per euro intensities
              pe_co2_kg = total_co2_kg/(total_fd_me*1000000),
              pe_co2_dom_kg = total_co2_dom_kg/(total_fd_me*1000000),
              pe_co2_eu_kg = total_co2_eu_kg/(total_fd_me*1000000),
              pe_co2_direct_kg = total_co2_direct_kg/(total_fd_me*1000000),
              pe_co2_noneu_kg = total_co2_noneu_kg/(total_fd_me*1000000),
              pe_co2eq_kg = total_co2eq_kg/(total_fd_me*1000000),
              pe_co2eq_dom_kg = total_co2eq_dom_kg/(total_fd_me*1000000),
              pe_co2eq_eu_kg = total_co2eq_eu_kg/(total_fd_me*1000000),
              pe_co2eq_direct_kg = total_co2eq_direct_kg/(total_fd_me*1000000),
              pe_co2eq_noneu_kg = total_co2eq_noneu_kg/(total_fd_me*1000000),
              pe_energy_use_mj = total_energy_use_tj/(total_fd_me),
              pe_energy_use_dom_mj = total_energy_use_dom_tj/(total_fd_me),
              pe_energy_use_eu_mj = total_energy_use_eu_tj/(total_fd_me),
              pe_energy_use_direct_mj = total_energy_use_direct_tj/(total_fd_me),
              pe_energy_use_noneu_mj = total_energy_use_noneu_tj/(total_fd_me)
    )
  dat_sectors_by_eu_ntile
}



# pivot results and add indicator type and eu ntile columns
pivot_results_longer <- function(pdat_in, pcols_exclude) {
  ## get iso3 codes to attach
  country_codes = ISOcodes::ISO_3166_1 %>%
    select(iso2 = Alpha_2, iso3 = Alpha_3) %>%
    mutate(iso2 = if_else(iso2=="GR", "EL", iso2)) %>%
    mutate(iso2 = if_else(iso2=="GB", "UK", iso2))
  
  # pivot longer (exlcuding paramter columns) and attach indicator names
  dat_out = pdat_in %>%
    pivot_longer(cols = -all_of(pcols_exclude), 
                 names_to = "indicator", 
                 values_to = "value") %>%
    mutate(indicator_type = case_when(grepl("total_", indicator) ~ "total",
                                      grepl("pae_", indicator) ~ "per_ae",
                                      grepl("pe_", indicator) ~ "intensity",
                                      TRUE ~ "other"))
  
  
  
  # if iso2 country code is present, add iso3
  if("iso2" %in% colnames(dat_out)) {
    dat_out = dat_out %>%
      left_join(country_codes, by="iso2")
  }
  # if the eu_q_rank column exists, provide a factor column
  if("eu_q_rank" %in% colnames(dat_out)) {
    dat_out = dat_out %>%
      mutate(eu_ntile_name = if_else(eu_q_rank<10, 
                                     paste0("Q0",eu_q_rank), 
                                     paste0("Q",eu_q_rank)))
  }
  dat_out
}

pivot_results_longer_adorn <- function(pdat_in, pcols_exclude) {
  dat_out = pivot_results_longer(pdat_in, pcols_exclude) %>%
    mutate(indicator_name = case_when(
      # per adult equivalent indicators
      indicator == "pae_co2_t" ~ "CO2 footprint (t/ae)",
      indicator == "pae_co2_dom_t" ~ "CO2 fp dom (t/ae)",
      indicator == "pae_co2_eu_t" ~ "CO2 fp EU (t/ae)",
      indicator == "pae_co2_noneu_t" ~ "CO2 fp non-EU (t/ae)",
      indicator == "pae_co2eq_t" ~ "CO2eq footprint (t/ae)",
      indicator == "pae_co2eq_dom_t" ~ "CO2eq fp dom (t/ae)",
      indicator == "pae_co2eq_eu_t" ~ "CO2eq fp EU (t/ae)",
      indicator == "pae_co2eq_noneu_t" ~ "CO2eq fp non-EU (t/ae)",
      indicator == "pae_energy_use_gj" ~ "Energy footprint (GJ/ae)",
      indicator == "pae_energy_use_dom_gj" ~ "Energy fp dom (GJ/ae)",
      indicator == "pae_energy_use_eu_gj" ~ "Energy fp EU (GJ/ae)",
      indicator == "pae_energy_use_noneu_gj" ~ "Energy fp non-EU (GJ/ae)",
      indicator == "pae_fd_ke" ~ "Expenditure (k€/ae)",
      # intensity indicators
      indicator == "pe_co2_kg" ~ "CO2 intensity (kg/€)",
      indicator == "pe_co2_dom_kg" ~ "CO2 intensity dom (kg/€)",
      indicator == "pe_co2_eu_kg" ~ "CO2 intensity EU (kg/€)",
      indicator == "pe_co2_noneu_kg" ~ "CO2 intensity non-EU (kg/€)",
      indicator == "pe_co2eq_kg" ~ "CO2eq intensity (kg/€)",
      indicator == "pe_co2eq_dom_kg" ~ "CO2eq intensity dom (kg/€)",
      indicator == "pe_co2eq_eu_kg" ~ "CO2eq intensity EU (kg/€)",
      indicator == "pe_co2eq_noneu_kg" ~ "CO2eq intensity non-EU (kg/€)",
      indicator == "pe_energy_use_mj" ~ "Energy intensity (MJ/€)",
      indicator == "pe_energy_use_dom_mj" ~ "Energy intensity dom (MJ/€)",
      indicator == "pe_energy_use_eu_mj" ~ "Energy intensity EU (MJ/€)",
      indicator == "pe_energy_use_noneu_mj" ~ "Energy intensity non-EU (MJ/€)"
    ))
  
}

get_eu_ntile_summary_long_adorned <- function(peu_q_count) {
  
  pdat_totals_eu_ntiles = get_country_summary_by_eu_ntile(peu_q_count) %>%
    group_by(year, eu_q_rank) %>%
    summarise(across(c(total_fd_me, 
                       total_adult_eq, 
                       total_co2_kg, 
                       total_co2_dom_kg, 
                       total_co2_eu_kg, 
                       total_co2_noneu_kg,
                       total_co2eq_kg, 
                       total_co2eq_dom_kg, 
                       total_co2eq_eu_kg, 
                       total_co2eq_noneu_kg,
                       total_energy_use_tj,
                       total_energy_use_dom_tj,
                       total_energy_use_eu_tj,
                       total_energy_use_noneu_tj
    ), sum)) %>%
    pivot_results_longer(c("year", "eu_q_rank") ) %>%
    mutate(value = case_when(indicator == "total_fd_me" ~ value/1000000,
                             indicator == "total_adult_eq" ~ value/1000000,
                             indicator == "total_co2_kg" ~ value/1000000000,
                             indicator == "total_co2_dom_kg" ~ value/1000000000,
                             indicator == "total_co2_eu_kg" ~ value/1000000000,
                             indicator == "total_co2_noneu_kg" ~ value/1000000000,
                             indicator == "total_co2eq_kg" ~ value/1000000000,
                             indicator == "total_co2eq_dom_kg" ~ value/1000000000,
                             indicator == "total_co2eq_eu_kg" ~ value/1000000000,
                             indicator == "total_co2eq_noneu_kg" ~ value/1000000000,
                             indicator == "total_energy_use_tj" ~ value/1000000,
                             indicator == "total_energy_use_dom_tj" ~ value/1000000,
                             indicator == "total_energy_use_eu_tj" ~ value/1000000,
                             indicator == "total_energy_use_noneu_tj" ~ value/1000000)) %>%
    mutate(indicator_name = case_when(indicator == "total_fd_me" ~ "Expenditure (trn €)",
                                      indicator == "total_adult_eq" ~ "Aeq Population (mio)",
                                      indicator == "total_co2_kg" ~ "CO2 footprint (Mt)",
                                      indicator == "total_co2_dom_kg" ~ "CO2 fp dom (Mt)",
                                      indicator == "total_co2_eu_kg" ~ "CO2 fp EU (Mt)",
                                      indicator == "total_co2_noneu_kg" ~ "CO2 fp non-EU (Mt)",
                                      indicator == "total_co2eq_kg" ~ "CO2eq footprint (Mt)",
                                      indicator == "total_co2eq_dom_kg" ~ "CO2eq fp dom (Mt)",
                                      indicator == "total_co2eq_eu_kg" ~ "CO2eq fp EU (Mt)",
                                      indicator == "total_co2eq_noneu_kg" ~ "CO2eq fp non-EU (Mt)",
                                      indicator == "total_energy_use_tj" ~ "Energy footprint (EJ)",
                                      indicator == "total_energy_use_dom_tj" ~ "Energy fp dom (EJ)",
                                      indicator == "total_energy_use_eu_tj" ~ "Energy fp EU (EJ)",
                                      indicator == "total_energy_use_noneu_tj" ~ "Energy fp non-EU (EJ)"
    )) %>%
    ungroup() %>%
    mutate(indicator_name = factor(indicator_name,
                                   levels=c("CO2 footprint (Mt)",
                                            "CO2eq footprint (Mt)",
                                            "Energy footprint (EJ)",
                                            "CO2 fp dom (Mt)",
                                            "CO2eq fp dom (Mt)",
                                            "Energy fp dom (EJ)",
                                            "CO2 fp EU (Mt)",
                                            "CO2eq fp EU (Mt)",
                                            "Energy fp EU (EJ)",
                                            "CO2 fp non-EU (Mt)",
                                            "CO2eq fp non-EU (Mt)",
                                            "Energy fp non-EU (EJ)",
                                            "Expenditure (trn €)",
                                            "Aeq Population (mio)"
                                   )))
  pdat_totals_eu_ntiles
}

get_eu_ntile_summary_relative_long_adorned <- function(peu_q_count) {
  pdat_relative_eu_ntile = get_country_summary_by_eu_ntile(peu_q_count) %>%
    group_by(year, eu_q_rank) %>%
    summarise(across(c(total_fd_me, 
                       total_co2_kg, 
                       total_co2_dom_kg,
                       total_co2_eu_kg,
                       total_co2_noneu_kg,
                       total_co2eq_kg, 
                       total_co2eq_dom_kg,
                       total_co2eq_eu_kg,
                       total_co2eq_noneu_kg,
                       total_energy_use_tj,
                       total_energy_use_dom_tj,
                       total_energy_use_eu_tj,
                       total_energy_use_noneu_tj,
    ), sum)) %>%
    group_by(year) %>%
    mutate(total_fd_me = total_fd_me/sum(total_fd_me)*100,
           total_co2_kg = total_co2_kg/sum(total_co2_kg)*100,
           total_co2_dom_kg = total_co2_dom_kg/sum(total_co2_dom_kg)*100,
           total_co2_eu_kg = total_co2_eu_kg/sum(total_co2_eu_kg)*100,
           total_co2_noneu_kg = total_co2_noneu_kg/sum(total_co2_noneu_kg)*100,
           total_co2eq_kg = total_co2eq_kg/sum(total_co2eq_kg)*100,
           total_co2eq_dom_kg = total_co2eq_dom_kg/sum(total_co2eq_dom_kg)*100,
           total_co2eq_eu_kg = total_co2eq_eu_kg/sum(total_co2eq_eu_kg)*100,
           total_co2eq_noneu_kg = total_co2eq_noneu_kg/sum(total_co2eq_noneu_kg)*100,
           total_energy_use_tj = total_energy_use_tj/sum(total_energy_use_tj)*100,
           total_energy_use_dom_tj = total_energy_use_dom_tj/sum(total_energy_use_dom_tj)*100,
           total_energy_use_eu_tj = total_energy_use_eu_tj/sum(total_energy_use_eu_tj)*100,
           total_energy_use_noneu_tj = total_energy_use_noneu_tj/sum(total_energy_use_noneu_tj)*100
    ) %>%
    pivot_longer(-c(year,eu_q_rank), names_to = "indicator", values_to = "value") %>%
    mutate(euro_quint = if_else(eu_q_rank<10, 
                                paste0("Q0",eu_q_rank), 
                                paste0("Q",eu_q_rank))) %>%
    mutate(indicator = case_when(indicator == "total_fd_me" ~ "Expenditure (%)",
                                 indicator == "total_co2_kg" ~ "CO2 footprint (%)",
                                 indicator == "total_co2_dom_kg" ~ "CO2 fp dom (%)",
                                 indicator == "total_co2_eu_kg" ~ "CO2 fp EU (%)",
                                 indicator == "total_co2_noneu_kg" ~ "CO2 fp non-EU (%)",
                                 indicator == "total_co2eq_kg" ~ "CO2eq footprint (%)",
                                 indicator == "total_co2eq_dom_kg" ~ "CO2eq fp dom (%)",
                                 indicator == "total_co2eq_eu_kg" ~ "CO2eq fp EU (%)",
                                 indicator == "total_co2eq_noneu_kg" ~ "CO2eq fp non-EU (%)",
                                 indicator == "total_energy_use_tj" ~ "Energy footprint (%)",
                                 indicator == "total_energy_use_dom_tj" ~ "Energy fp dom (%)",
                                 indicator == "total_energy_use_eu_tj" ~ "Energy fp EU (%)",
                                 indicator == "total_energy_use_noneu_tj" ~ "Energy fp non-EU (%)")) %>%
    ungroup() %>%
    mutate(indicator = factor(indicator,
                              levels=c("CO2 footprint (%)",
                                       "CO2eq footprint (%)",
                                       "Energy footprint (%)",
                                       "CO2 fp dom (%)",
                                       "CO2eq fp dom (%)",
                                       "Energy fp dom (%)",
                                       "CO2 fp EU (%)",
                                       "CO2eq fp EU (%)",
                                       "Energy fp EU (%)",
                                       "CO2 fp non-EU (%)",
                                       "CO2eq fp non-EU (%)",
                                       "Energy fp non-EU (%)",
                                       "Expenditure (%)"
                              )))
  pdat_relative_eu_ntile
}