library(dplyr) # for data wrangling
library(tidyr)
library(ggplot2) # for plots
library(magrittr) # for pipe %>%
library(MASS)
#changing working direction
setwd("c:/Econ 390")
#open dataset
data <- read.csv("C:/Econ 390/assignment 1 data.csv")
head(data)
## State Facility.Name Facility.ID Year Gross.Load..MWh. Steam.Load..1000.lb.
## 1 AL Barry 3 2000 12787607 NA
## 2 AL Barry 3 2001 16137285 NA
## 3 AL Barry 3 2002 17931286 NA
## 4 AL Barry 3 2003 15475217 NA
## 5 AL Barry 3 2004 15516154 NA
## 6 AL Barry 3 2005 15471435 NA
## SO2.Mass..short.tons. CO2.Mass..short.tons. NOx.Mass..short.tons.
## 1 61331.11 12435918 23667.13
## 2 65902.38 13683576 23026.47
## 3 59524.80 14700456 24400.65
## 4 52706.28 13941717 22451.15
## 5 47470.36 13217576 19312.65
## 6 53652.94 13716973 19248.53
## Heat.Input..mmBtu.
## 1 124857223
## 2 147934446
## 3 162155771
## 4 146353476
## 5 140814946
## 6 143241269
data<-data %>% dplyr::select(State, Facility.Name, Year, Gross.Load..MWh., CO2.Mass..short.tons.)
head(data)
## State Facility.Name Year Gross.Load..MWh. CO2.Mass..short.tons.
## 1 AL Barry 2000 12787607 12435918
## 2 AL Barry 2001 16137285 13683576
## 3 AL Barry 2002 17931286 14700456
## 4 AL Barry 2003 15475217 13941717
## 5 AL Barry 2004 15516154 13217576
## 6 AL Barry 2005 15471435 13716973
data<- data %>%
rename("Facility"="Facility.Name")
head(data)
## State Facility Year Gross.Load..MWh. CO2.Mass..short.tons.
## 1 AL Barry 2000 12787607 12435918
## 2 AL Barry 2001 16137285 13683576
## 3 AL Barry 2002 17931286 14700456
## 4 AL Barry 2003 15475217 13941717
## 5 AL Barry 2004 15516154 13217576
## 6 AL Barry 2005 15471435 13716973
str(data)
## 'data.frame': 32517 obs. of 5 variables:
## $ State : chr "AL" "AL" "AL" "AL" ...
## $ Facility : chr "Barry" "Barry" "Barry" "Barry" ...
## $ Year : int 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 ...
## $ Gross.Load..MWh. : num 12787607 16137285 17931286 15475217 15516154 ...
## $ CO2.Mass..short.tons.: num 12435918 13683576 14700456 13941717 13217576 ...
data_long <- gather(data, Measure, Value,-c(State,Facility,Year))
head(data_long)
## State Facility Year Measure Value
## 1 AL Barry 2000 Gross.Load..MWh. 12787607
## 2 AL Barry 2001 Gross.Load..MWh. 16137285
## 3 AL Barry 2002 Gross.Load..MWh. 17931286
## 4 AL Barry 2003 Gross.Load..MWh. 15475217
## 5 AL Barry 2004 Gross.Load..MWh. 15516154
## 6 AL Barry 2005 Gross.Load..MWh. 15471435
tail(data_long)
## State Facility Year Measure Value
## 65029 IN Grain Processing Corporation 2022 CO2.Mass..short.tons. NA
## 65030 OH Pratt Paper (OH) 2019 CO2.Mass..short.tons. NA
## 65031 OH Pratt Paper (OH) 2020 CO2.Mass..short.tons. NA
## 65032 OH Pratt Paper (OH) 2021 CO2.Mass..short.tons. NA
## 65033 OH Pratt Paper (OH) 2022 CO2.Mass..short.tons. NA
## 65034 TN Holston Army Ammunition Plant 2022 CO2.Mass..short.tons. NA
data_wide <- spread(data_long, Measure, Value)
head(data_wide)
## State Facility Year CO2.Mass..short.tons. Gross.Load..MWh.
## 1 AK Healy Power Plant 2019 NA 236165.5
## 2 AK Healy Power Plant 2020 NA 214289.1
## 3 AK Healy Power Plant 2021 NA 250187.9
## 4 AK Healy Power Plant 2022 NA 289925.7
## 5 AL AMEA Sylacauga Plant 2004 4304.2 8091.0
## 6 AL AMEA Sylacauga Plant 2005 5354.3 12735.0
tail(data_wide)
## State Facility Year CO2.Mass..short.tons. Gross.Load..MWh.
## 32512 WY Wyodak 2017 3380149 2901325
## 32513 WY Wyodak 2018 2988233 2575909
## 32514 WY Wyodak 2019 2449066 2122348
## 32515 WY Wyodak 2020 2338068 2010443
## 32516 WY Wyodak 2021 2321383 1984993
## 32517 WY Wyodak 2022 2353445 2021599
dataset<-data %>% dplyr::select(State, Facility, Year, CO2.Mass..short.tons.)
head(dataset)
## State Facility Year CO2.Mass..short.tons.
## 1 AL Barry 2000 12435918
## 2 AL Barry 2001 13683576
## 3 AL Barry 2002 14700456
## 4 AL Barry 2003 13941717
## 5 AL Barry 2004 13217576
## 6 AL Barry 2005 13716973
#Step 2 h. Remove all rows with “NA” (that is missing values).
dataset<- dataset %>%filter(!is.na(CO2.Mass..short.tons.))
#Step 2 i. Calculate the Mean and SD of CO2 by State
CO2_Summary_By_State <- dataset %>%
group_by(State) %>%
summarise(Mean=mean(CO2.Mass..short.tons.), SD=sd(CO2.Mass..short.tons.))
CO2_Summary_By_State
## # A tibble: 49 × 3
## State Mean SD
## <chr> <dbl> <dbl>
## 1 AL 3398371. 5191486.
## 2 AR 2002796. 3415015.
## 3 AZ 2342109. 3981856.
## 4 CA 416463. 657256.
## 5 CO 1776613. 2509586.
## 6 CT 489834. 729479.
## 7 DC 116471. 83370.
## 8 DE 470526. 869900.
## 9 FL 2060763. 2845345.
## 10 GA 2331124. 5120023.
## # ℹ 39 more rows
#Step 2 j. Repeat the calculation of Mean and SD of CO2 by Year
CO2_Summary_By_Year <- dataset %>%
group_by(Year) %>%
summarise(Mean=mean(CO2.Mass..short.tons.), SD=sd(CO2.Mass..short.tons.))
CO2_Summary_By_Year
## # A tibble: 23 × 3
## Year Mean SD
## <int> <dbl> <dbl>
## 1 2000 2964806. 4151513.
## 2 2001 2550567. 3858703.
## 3 2002 2215485. 3722301.
## 4 2003 2114542. 3681553.
## 5 2004 2105665. 3688322.
## 6 2005 2108128. 3718066.
## 7 2006 2040767. 3678880.
## 8 2007 2065805. 3705808.
## 9 2008 1959587. 3627240.
## 10 2009 1834199. 3410922.
## # ℹ 13 more rows
#Step 2 i. Calculate the Mean and SD of CO2 by State
CO2_Summary_By_State <- dataset %>%
group_by(State) %>%
summarise(Mean=mean(CO2.Mass..short.tons.), SD=sd(CO2.Mass..short.tons.))
CO2_Summary_By_State
## # A tibble: 49 × 3
## State Mean SD
## <chr> <dbl> <dbl>
## 1 AL 3398371. 5191486.
## 2 AR 2002796. 3415015.
## 3 AZ 2342109. 3981856.
## 4 CA 416463. 657256.
## 5 CO 1776613. 2509586.
## 6 CT 489834. 729479.
## 7 DC 116471. 83370.
## 8 DE 470526. 869900.
## 9 FL 2060763. 2845345.
## 10 GA 2331124. 5120023.
## # ℹ 39 more rows
#Step 2 k. Subset the data for only 2022 (that is have only for Year 2022). Subset for 2021 as well.This can be done with filter command. Then rename co2 column for 2022 to co2_2022 and co2 for 2021 to co2_2021.
data_2022<-dataset %>%filter(Year==2022)
data_2021<-dataset %>%filter(Year==2021)
data_2022<- data_2022 %>%rename("co2_2022"="CO2.Mass..short.tons.")
data_2021<- data_2021 %>%rename("co2_2021"="CO2.Mass..short.tons.")
head(data_2022)
## State Facility Year co2_2022
## 1 AL Barry 2022 7588387.10
## 2 AL Gadsden 2022 1246.25
## 3 AL Greene County 2022 817274.85
## 4 AL E C Gaston 2022 3823447.10
## 5 AL Colbert 2022 31752.60
## 6 KY Smith Generating Facility 2022 533966.87
head(data_2021)
## State Facility Year co2_2021
## 1 AL Barry 2021 7577398.0
## 2 AL Greene County 2021 506935.5
## 3 AL E C Gaston 2021 4527563.6
## 4 AL Colbert 2021 16295.2
## 5 LA Dolet Hills Power Station 2021 1408050.2
## 6 KY Smith Generating Facility 2021 416952.0
#Step 2 l.Left join these two data sets (data for 2021 and 2022)
dataleft<-left_join(data_2022,data_2021,by=c('State','Facility'))
head(dataleft)
## State Facility Year.x co2_2022 Year.y co2_2021
## 1 AL Barry 2022 7588387.10 2021 7577398.0
## 2 AL Gadsden 2022 1246.25 NA NA
## 3 AL Greene County 2022 817274.85 2021 506935.5
## 4 AL E C Gaston 2022 3823447.10 2021 4527563.6
## 5 AL Colbert 2022 31752.60 2021 16295.2
## 6 KY Smith Generating Facility 2022 533966.87 2021 416952.0