knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(tidyverse)
library(devtools)
library(testthat)
library(Assignment5)

This is assignment 5 for ESM 262 - Environmental Computing. We created multiple functions using data about US CO2 emissions from different industries and different emission sources. Additionaly, for some areas to run smoother we created data. This was to understand how to run certain parts of the assignment like arrays.

There are 2 functions summarizing the data, 2 functions calculating the impacts of CO2 emissions, one calculating the tax cost of emissions using arrays of fake data. All functions have an associated help page at ?help and cane be found in the "R" subdirectory. All functions are demonstrated below. We also have 3 tests in a "tests" subdirectory on functions that use the "ghg" data.

Function Descriptions:

#get data (saved in data subdirectory as .rda file)
data(ghg)

Summarize: Average natural gas emissions by year

#use ghg dataset as your argument and filter to see mean emissions of natural gas over the 7 years of data
average_ng= function(ghg_data) {

   ng_emissions = ghg_data %>%
    dplyr::filter(Fuel_Type == "Natural Gas") %>%
    dplyr::select(Total_CO2_Emissions) %>%
    dplyr::summarize(mean = (sum(Total_CO2_Emissions)/7))

  return(ng_emissions$mean)
}

Summarize: Add up total emisisons by year

#use ghg dataset. Group by year and find sum total emissions of each year in chronological order
add_total_emissions_year= function(ghg_data) {

    annual_emissions = ghg_data %>%
    dplyr::group_by(Year) %>%
    dplyr::summarize(sum_col = sum(Total_CO2_Emissions)) %>%
    dplyr::arrange(-Year) %>%
    head(10)

  return(annual_emissions)
}

#See decreasing trend in CO2 emissions

Array: If each ton of carbon is taxed $1000 how much would each sector owe?

#build array of emissions data with columns as sectors, rows as fuel type, and arrays as the years 2010 and 2016.
emissions_year = array(1:40, dim=c(5,4,2), #fill array with numbers 1-40 FAKE DATA
                dimnames = list(c('Transportation', 'Electricity_Generation', 'Residential', 'Commercial',  'Industrial'), # add column names
              c('Diesel', 'Gasoline', 'Coal', 'Natural Gas'), #add row names
              c(2010,2016))) #one array for 2010, one for 2016


#each ton is taxed $1,000, use function to find economic cost for emissions for each sector in 2010 and 2016. Data was created for array in above code so data is not accurate.
calculate_cost = function(emissions_year) {

  cost =  apply(emissions_year, c(1,3), sum)*1000
  return(cost)#apply tax to sum of each secters emissions for both years
  }

#Based on this data, industrial facilities emit the most CO2 and will be taxed the most

Calculate Impact: 10 tons of CO2 emissions cause warming of .1 degree C

#use ghg dataset and select data on total co2 emissions accross all sectors and years. Divide by 10 since and multiply by .1 to get expected warming. 
calc_total_warming = function(ghg_data) {

  global_warming= ghg_data %>%
    dplyr::select(Total_CO2_Emissions) %>%
    dplyr::summarize(total = (sum(Total_CO2_Emissions/10*0.1)))

  return(global_warming$total)
}

#result is the total warming expected over 2010-2016 for all emissions in that time

Calculate warming if coal emissions were replaced with renewable (zero emissions) sources

#use ghg dataset and select emissions from all fuel types except for coal. Select data on total co2 emissions accross all sectors and years. Divide by 10 since and multiply by .1 to get expected warming. 
calc_warming_no_coal = function(ghg_data) {

  less_emissions = ghg_data %>%
    dplyr::filter(Fuel_Type != "Coal") %>%
    dplyr::select(Total_CO2_Emissions) %>%
    dplyr::summarize(total =(sum(Total_CO2_Emissions)/10*.1))



  return(less_emissions)
}

#result is the total warming expected over 2010-2016 for all non-coal emissions in that time


angiebouche/ESM_262_Assignment_5 documentation built on June 14, 2019, 3:25 p.m.