IDEMSInternational / carbonr

Calculating carbon emissions in R
GNU Lesser General Public License v3.0
14 stars 2 forks source link

Add "materials" and "waste" functions #13

Closed lilyclements closed 2 months ago

lilyclements commented 2 years ago

Two tabs on the 2021 report not yet considered fully:

Need to think about how to best do this. "Material Use" in the document is currently by tonnes used. However, knowing the tonnes of computers or papers a company used in a year is difficult.

carbonfootprint.com calculates it by using cost spent on products - could be a good way to do it here too. Using the values in carbonfootprint.com gives the following R code. This is for manufacture, delivery, and disposal of goods. In the UK govt document, all three of these options are on separate sheets.

#' Calculate CO2e emissions from secondary sources
#' 
#' @description CO2e emissions from the manufacture, delivery, and disposal of products and services in a year. Emission values are calculated from carbonfootprint.com, which base the emissions values off DEFRA 2017 Supply Chain Factors.
#'
#' @param item Item bought. Valid options are: `"IT equipment"`, `"restaurants"`, `"TV"`, `"radio"`, `"phone"`, `"paper based products"`, `"motor"`, `"manufactured goods"`, `"telephone/mobile call costs"`, `"banking/finance"`, `"insurance"`, `"education"`, `"recreational activities"`, `"textiles"`, `"food"`.
#' @param cost Amount spent on that item (in pounds)
#' @param time Time frame where the purchases took place. Options are `"per week"`, `"per month"`, `"per year"`.
#' @param diet Only if `item = "food"`. General diet of an individual. Options are `"high meat"`, `"medium meat"`, `"low meat"`, `"pescatarian"`, `"vegetarian"`, and `"vegan"`. 
#' `"high meat"` suggests more than 100g of meat is consumed a day; `"medium meat"` for 50-100g/day; `"low meat"` for <50g/day.
#'
#' @return Tonnes of CO2 emissions from secondary sources in a year.
#' @export
#'
#' @examples # Emissions for a new £1000 computer. 
#' secondary_emissions(item = "IT equipment", cost = 1000)
#' @examples # Emissions for a high meat eater who spends £200 on food each month. 
#' secondary_emissions(item = "food", cost = 200, time = "per month")

# put in data set and call from that data set.
secondary_emissions <- function(item = c("IT equipment", "restaurants", "TV", "radio", "phone", "paper based products", "motor", "manufactured goods", "food",
                                         "telephone/mobile call costs", "banking/finance", "insurance", "education", "recreational activities", "textiles"),
                                cost, time = c("per week", "per month", "per year"),
                                diet = c("medium meat", "high meat", "low meat", "pescatarian", "vegetarian", "vegan")){

  item_list <- c("IT equipment", "restaurants", "TV", "radio", "phone", "paper based products", "motor", "manufactured goods", "telephone/mobile call costs",
                 "banking/finance", "insurance", "education", "recreational activities", "textiles")

  item <- match.arg(item)
  diet <- match.arg(diet)
  time <- match.arg(time)
  checkmate::assert_numeric(cost, lower = 0)

  # cost list is cost per £1 spent
  CO2e <- c(0.00114, 0.00037, 0.00114, 0.00114, 0.00114, 0.00027, 0.00030, 0.00031, 0.00024, 0.00039, 0.00018, 0.00025, 0.00032, 0.00040)
  df_list <- data.frame(item_list, CO2e)

  if (item == "food"){
    if (diet == "high meat"){
      emissions <- 0.00088
    } else if (diet == "medium meat"){
      emissions <- 0.00069
    } else if (diet == "low meat"){
      emissions <- 0.00057
    } else if (diet == "pescatarian"){
      emissions <- 0.00048
    } else if (diet == "vegetarian"){
      emissions <- 0.00047
    } else if (diet == "vegan"){
      emissions <- 0.00035
    }
  } else {
    row <- which(df_list$item_list == item)
    emissions <- df_list$CO2e[row]
  }
  if (time == "per year"){
    time_factor <- 1
  } else if (time == "per month") {
    time_factor <- 12
  } else if (time == "per week") {
    time_factor <- 52
  }
  emissions <- emissions * cost * time_factor
  return(emissions)
}