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)
#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) }
#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
#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
#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
#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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.