From b01d566003dd8c68df1d59cd9afc0120828ee7c1 Mon Sep 17 00:00:00 2001 From: CJ Yetman - RMI Date: Fri, 5 Apr 2024 00:28:39 +0200 Subject: [PATCH 1/2] use `pacta.data.preparation::prepare_masterdata_debt()` --- run_pacta_data_preparation.R | 39 +++++++----------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/run_pacta_data_preparation.R b/run_pacta_data_preparation.R index dc6acb4..bd44c9e 100644 --- a/run_pacta_data_preparation.R +++ b/run_pacta_data_preparation.R @@ -357,41 +357,16 @@ readr::read_csv(masterdata_ownership_path, na = "", show_col_types = FALSE) %>% invisible(gc()) -logger::log_info( - "Formatting and saving file: \"masterdata_debt_datastore.rds\"." -) - -masterdata_debt <- readr::read_csv(masterdata_debt_path, na = "", show_col_types = FALSE) - -company_id__creditor_company_id <- - pacta.data.preparation::prepare_company_id__creditor_company_id(masterdata_debt) +logger::log_info("Formatting and saving file: \"masterdata_debt_datastore.rds\".") -masterdata_debt %>% - pacta.data.preparation::prepare_masterdata( - ar_company_id__country_of_domicile, - config[["pacta_financial_timestamp"]], - config[["zero_emission_factor_techs"]] - ) %>% - left_join(company_id__creditor_company_id, by = c(id = "company_id")) %>% - left_join(ar_company_id__credit_parent_ar_company_id, by = c(id = "ar_company_id")) %>% - mutate(id = if_else(!is.na(.data$credit_parent_ar_company_id), .data$credit_parent_ar_company_id, .data$id)) %>% - mutate(id = if_else(!is.na(.data$creditor_company_id), .data$creditor_company_id, .data$id)) %>% - mutate(id_name = "credit_parent_ar_company_id") %>% - group_by( - .data$id, .data$id_name, .data$ald_sector, .data$ald_location, - .data$technology, .data$year, .data$country_of_domicile, - .data$ald_production_unit, .data$ald_emissions_factor_unit, - ) %>% - summarise( - ald_emissions_factor = stats::weighted.mean(.data$ald_emissions_factor, .data$ald_production, na.rm = TRUE), - ald_production = sum(.data$ald_production, na.rm = TRUE), - .groups = "drop" - ) %>% +pacta.data.preparation::prepare_masterdata_debt( + masterdata_debt_raw = readr::read_csv(masterdata_debt_path, na = "", show_col_types = FALSE), + ar_company_id__credit_parent_ar_company_id = ar_company_id__credit_parent_ar_company_id, + pacta_financial_timestamp = config[["pacta_financial_timestamp"]], + zero_emission_factor_techs = config[["zero_emission_factor_techs"]] +) %>% saveRDS(file.path(config[["data_prep_outputs_path"]], "masterdata_debt_datastore.rds")) -invisible(gc()) -rm(masterdata_debt) -rm(company_id__creditor_company_id) invisible(gc()) rm(ar_company_id__country_of_domicile) From 413960ba17f8c418f914522c564fc5f26f7430fd Mon Sep 17 00:00:00 2001 From: CJ Yetman - RMI Date: Fri, 5 Apr 2024 00:30:06 +0200 Subject: [PATCH 2/2] add `ar_company_id__country_of_domicile` --- run_pacta_data_preparation.R | 1 + 1 file changed, 1 insertion(+) diff --git a/run_pacta_data_preparation.R b/run_pacta_data_preparation.R index bd44c9e..2c5945e 100644 --- a/run_pacta_data_preparation.R +++ b/run_pacta_data_preparation.R @@ -361,6 +361,7 @@ logger::log_info("Formatting and saving file: \"masterdata_debt_datastore.rds\". pacta.data.preparation::prepare_masterdata_debt( masterdata_debt_raw = readr::read_csv(masterdata_debt_path, na = "", show_col_types = FALSE), + ar_company_id__country_of_domicile = ar_company_id__country_of_domicile, ar_company_id__credit_parent_ar_company_id = ar_company_id__credit_parent_ar_company_id, pacta_financial_timestamp = config[["pacta_financial_timestamp"]], zero_emission_factor_techs = config[["zero_emission_factor_techs"]]