From 994d70ae804ff0929f0df372babe7f61c5a1d24e Mon Sep 17 00:00:00 2001 From: Kalash Singhal <125359076+kalashsinghal@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:34:04 +0200 Subject: [PATCH] Update methodology for co2 footprint per benchmark (#45) * Update methodology for co2 footprint per benchmark * refactor * refactor --- DESCRIPTION | 2 +- ...e_co2_descriptive_analysis_per_benchmark.R | 34 +++++++++---------- R/example_data.R | 18 +++++----- ..._co2_descriptive_analysis_per_benchmark.Rd | 13 ++++--- ...e-analysis-co2_footprint_per_benchmark.Rmd | 4 +-- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ea6f2b8..74de41b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,7 +11,7 @@ URL: https://github.com/2DegreesInvesting/tiltDataAnalysis, BugReports: https://github.com/2DegreesInvesting/tiltDataAnalysis/issues Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Suggests: testthat (>= 3.0.0) Config/testthat/edition: 3 diff --git a/R/calculate_co2_descriptive_analysis_per_benchmark.R b/R/calculate_co2_descriptive_analysis_per_benchmark.R index ee9fec3..4e7fa5c 100644 --- a/R/calculate_co2_descriptive_analysis_per_benchmark.R +++ b/R/calculate_co2_descriptive_analysis_per_benchmark.R @@ -1,10 +1,10 @@ -#' Calculate the descriptive analysis of co2_footprint per benchmark +#' Calculate the descriptive analysis of co2_footprint per grouping_emission #' #' This function calculates the descriptive analysis of co2_footprint per -#' benchmark using product-level output of Emissions profile indicator. +#' grouping_emission using product-level output of Emissions profile indicator. #' #' @param emission_product A dataframe. Product-level output of Emissions profile -#' @param benchmark A string. Benchmarks of Emissions profile +#' @param grouping_emission A string. Benchmarks of Emissions profile #' #' @return A dataframe #' @export @@ -14,37 +14,37 @@ #' emission_product_example <- example_emission_product_co2_des_analysis() #' emission_product_example #' -#' # Calculates the descriptive analysis for benchmark "tilt_subsector" +#' # Calculates the descriptive analysis for grouping_emission "tilt_subsector" #' calculate_co2_descriptive_analysis_per_benchmark( #' emission_product_example, #' "tilt_subsector" #' ) -calculate_co2_descriptive_analysis_per_benchmark <- function(emission_product, benchmark) { - if (benchmark == "all") { - select_benchmarks <- c("benchmark", "co2_footprint", "ep_product") +calculate_co2_descriptive_analysis_per_benchmark <- function(emission_product, grouping_emission) { + if (grouping_emission == "all") { + select_benchmarks <- c("grouping_emission", "co2_footprint", "activity_uuid_product_uuid") group_benchmark <- NULL - } else if (benchmark == "unit_isic_4digit") { - select_benchmarks <- c("unit", "isic_4digit", "benchmark", "co2_footprint", "ep_product") + } else if (grouping_emission == "unit_isic_4digit") { + select_benchmarks <- c("unit", "isic_4digit", "grouping_emission", "co2_footprint", "activity_uuid_product_uuid") group_benchmark <- c("unit", "isic_4digit") - } else if (benchmark == "unit_tilt_subsector") { - select_benchmarks <- c("tilt_sector", "unit", "tilt_subsector", "benchmark", "co2_footprint", "ep_product") + } else if (grouping_emission == "unit_tilt_subsector") { + select_benchmarks <- c("tilt_sector", "unit", "tilt_subsector", "grouping_emission", "co2_footprint", "activity_uuid_product_uuid") group_benchmark <- c("unit", "tilt_subsector") } else { - select_benchmarks <- c("tilt_sector", benchmark, "benchmark", "co2_footprint", "ep_product") - group_benchmark <- benchmark + select_benchmarks <- c("tilt_sector", grouping_emission, "grouping_emission", "co2_footprint", "activity_uuid_product_uuid") + group_benchmark <- grouping_emission } output <- emission_product |> select(all_of(select_benchmarks)) |> distinct() |> - filter(benchmark == benchmark) |> - select(-c("benchmark")) |> - mutate("# of distinct products" = n_distinct(.data$ep_product, na.rm = TRUE), .by = group_benchmark) |> + filter(grouping_emission == grouping_emission) |> + select(-c("grouping_emission")) |> + mutate("# of distinct products" = n_distinct(.data$activity_uuid_product_uuid, na.rm = TRUE), .by = group_benchmark) |> mutate("min CO2e" = min(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |> mutate("max CO2e" = max(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |> mutate("mean CO2e" = mean(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |> mutate("median CO2e" = median(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |> - select(-all_of(c("ep_product", "co2_footprint"))) |> + select(-all_of(c("activity_uuid_product_uuid", "co2_footprint"))) |> distinct() output[order(output[[1]], output[[2]]), ] diff --git a/R/example_data.R b/R/example_data.R index 861ea14..fb4a525 100644 --- a/R/example_data.R +++ b/R/example_data.R @@ -24,15 +24,15 @@ example_emission_product_co2_des_analysis <- function() { tribble( # styler: off - ~benchmark, ~tilt_sector, ~tilt_subsector, ~unit, ~isic_4digit, ~co2_footprint, ~ep_product, - "tilt_subsector", "group", "chemicals", "kg", "'5555'", 1, "water", - "tilt_subsector", "group", "chemicals", "kg", "'5555'", 4, "television", - "tilt_subsector", "group", "chemicals", "kg", "'5555'", 9, "tables", - "tilt_subsector", "group", "chemicals", "kg", "'5555'", 8, "electricity", - "tilt_subsector", "group", "chemicals", "kg", "'5555'", NA_real_, "meat", - "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 1, "tables", - "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 2, "water", - "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 4, "tent" + ~grouping_emission, ~tilt_sector, ~tilt_subsector, ~unit, ~isic_4digit, ~co2_footprint, ~product, ~activity_uuid_product_uuid, + "tilt_subsector", "group", "chemicals", "kg", "'5555'", 1, "water", "uuid1", + "tilt_subsector", "group", "chemicals", "kg", "'5555'", 4, "television", "uuid2", + "tilt_subsector", "group", "chemicals", "kg", "'5555'", 9, "tables", "uuid3", + "tilt_subsector", "group", "chemicals", "kg", "'5555'", 8, "electricity", "uuid4", + "tilt_subsector", "group", "chemicals", "kg", "'5555'", NA_real_, "meat", "uuid5", + "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 1, "tables", "uuid3", + "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 2, "water", "uuid1", + "tilt_subsector", "group", "iron & steel", "m3", "'6666'", 4, "tent", "uuid6" # styler: on ) } diff --git a/man/calculate_co2_descriptive_analysis_per_benchmark.Rd b/man/calculate_co2_descriptive_analysis_per_benchmark.Rd index 00d0949..01cd433 100644 --- a/man/calculate_co2_descriptive_analysis_per_benchmark.Rd +++ b/man/calculate_co2_descriptive_analysis_per_benchmark.Rd @@ -3,28 +3,31 @@ % R/calculate_co2_descriptive_analysis_per_benchmark.R \name{calculate_co2_descriptive_analysis_per_benchmark} \alias{calculate_co2_descriptive_analysis_per_benchmark} -\title{Calculate the descriptive analysis of co2_footprint per benchmark} +\title{Calculate the descriptive analysis of co2_footprint per grouping_emission} \usage{ -calculate_co2_descriptive_analysis_per_benchmark(emission_product, benchmark) +calculate_co2_descriptive_analysis_per_benchmark( + emission_product, + grouping_emission +) } \arguments{ \item{emission_product}{A dataframe. Product-level output of Emissions profile} -\item{benchmark}{A string. Benchmarks of Emissions profile} +\item{grouping_emission}{A string. Benchmarks of Emissions profile} } \value{ A dataframe } \description{ This function calculates the descriptive analysis of co2_footprint per -benchmark using product-level output of Emissions profile indicator. +grouping_emission using product-level output of Emissions profile indicator. } \examples{ library(dplyr) emission_product_example <- example_emission_product_co2_des_analysis() emission_product_example -# Calculates the descriptive analysis for benchmark "tilt_subsector" +# Calculates the descriptive analysis for grouping_emission "tilt_subsector" calculate_co2_descriptive_analysis_per_benchmark( emission_product_example, "tilt_subsector" diff --git a/vignettes/articles/descriptive-analysis-co2_footprint_per_benchmark.Rmd b/vignettes/articles/descriptive-analysis-co2_footprint_per_benchmark.Rmd index 3a4ad69..e5c9c92 100644 --- a/vignettes/articles/descriptive-analysis-co2_footprint_per_benchmark.Rmd +++ b/vignettes/articles/descriptive-analysis-co2_footprint_per_benchmark.Rmd @@ -71,7 +71,7 @@ kable(co2_tilt_subsector, align = "llccccc", caption = "Descriptive analysis for ```{r, echo=FALSE} lowest_co2 <- emission_product_example |> - select(all_of(c("ep_product", "co2_footprint"))) |> + select(all_of(c("product", "co2_footprint"))) |> distinct() |> arrange(.data$co2_footprint) |> head(5) @@ -85,7 +85,7 @@ kable(lowest_co2, align = "lc", caption = "Five products with lowest CO2e values ```{r, echo=FALSE} highest_co2 <- emission_product_example |> - select(all_of(c("ep_product", "co2_footprint"))) |> + select(all_of(c("product", "co2_footprint"))) |> distinct() |> arrange(desc(.data$co2_footprint)) |> head(5)