Skip to content

Commit

Permalink
Update methodology for co2 footprint per benchmark (#45)
Browse files Browse the repository at this point in the history
* Update methodology for co2 footprint per benchmark

* refactor

* refactor
  • Loading branch information
kalashsinghal authored Sep 2, 2024
1 parent c0a9905 commit 994d70a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions R/calculate_co2_descriptive_analysis_per_benchmark.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]]), ]
Expand Down
18 changes: 9 additions & 9 deletions R/example_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
13 changes: 8 additions & 5 deletions man/calculate_co2_descriptive_analysis_per_benchmark.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 994d70a

Please sign in to comment.