Code
library(dplyr)
library(here)
library(readr)
library(tidyverse)library(dplyr)
library(here)
library(readr)
library(tidyverse)For this project I will be importing the CO2_Emissions_Global_Trade.csv data set which was sourced from the IMF Climate Change Dashboard.
co2_emissions <- read_csv(here("data", "CO2_Emissions_Global_Trade.csv"))Rows: 402 Columns: 38
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (10): Country, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, ...
dbl (28): ObjectId, F1995, F1996, F1997, F1998, F1999, F2000, F2001, F2002, ...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(co2_emissions)# A tibble: 6 × 38
ObjectId Country ISO2 ISO3 Indicator Unit Source CTS_Code CTS_Name
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTDB CO2 Emi…
2 2 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTDD CO2 Emi…
3 3 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTTX CO2 Emi…
4 4 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTT CO2 Emi…
5 5 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTT CO2 Emi…
6 6 Argentina AR ARG CO2 Emissions E… Mill… "OECD… ECBTP CO2 Emi…
# ℹ 29 more variables: CTS_Full_Descriptor <chr>, Scale <chr>, F1995 <dbl>,
# F1996 <dbl>, F1997 <dbl>, F1998 <dbl>, F1999 <dbl>, F2000 <dbl>,
# F2001 <dbl>, F2002 <dbl>, F2003 <dbl>, F2004 <dbl>, F2005 <dbl>,
# F2006 <dbl>, F2007 <dbl>, F2008 <dbl>, F2009 <dbl>, F2010 <dbl>,
# F2011 <dbl>, F2012 <dbl>, F2013 <dbl>, F2014 <dbl>, F2015 <dbl>,
# F2016 <dbl>, F2017 <dbl>, F2018 <dbl>, F2019 <dbl>, F2020 <dbl>,
# F2021 <dbl>
Clean the data as needed using dplyr and related tidyverse packages.
To tidy this data, we are only going to select the important columns containing data. There is a lot of repetition and lengthy descriptions. We will keep the country name, indicator, CTS code, and columns for the years.
The years column also has an ‘F’ prefix that should be removed.
I also don’t like how the indicator is such a long string, we can mutate this column to be more easily parseable.
# Select the relevant columns
co2_emissions_tidy <- co2_emissions %>%
select(Country, Indicator, CTS_Code, 12:38)
names(co2_emissions_tidy) <- sub('^F', '', names(co2_emissions_tidy))
# Mutate the Indicator column to be more easily readable
co2_emissions_tidy <- co2_emissions_tidy %>%
mutate(Indicator = case_when(
Indicator == 'CO2 Emissions Embodied in Final Demand, balance' ~ 'CO2_final_demand_balance',
Indicator == 'CO2 Emissions Embodied in Final Domestic Demand' ~ 'CO2_final_domestic_demand',
Indicator == 'CO2 Emissions Embodied in Gross Exports' ~ 'CO2_gross_exports',
Indicator == 'CO2 Emissions Embodied in Gross Exports, balance' ~ 'CO2_gross_exports_balance',
Indicator == 'CO2 Emissions Embodied in Gross Imports' ~ 'CO2_gross_imports',
Indicator == 'CO2 Emissions Embodied in Production' ~ 'CO2_production'
))
head(co2_emissions_tidy)# A tibble: 6 × 30
Country Indicator CTS_Code `1995` `1996` `1997` `1998` `1999` `2000` `2001`
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Argentina CO2_final… ECBTDB -14.4 -15.6 -17.5 -21.8 -19.3 -18.1 -12.8
2 Argentina CO2_final… ECBTDD 124. 136. 141. 152. 151. 148. 136.
3 Argentina CO2_gross… ECBTTX 9.47 11.4 15.2 13.6 12.5 14.6 14.7
4 Argentina CO2_gross… ECBTT -14.4 -15.6 -17.5 -21.8 -19.3 -18.1 -12.8
5 Argentina CO2_gross… ECBTT 23.5 26.7 32.3 34.9 31.3 32.4 27.2
6 Argentina CO2_produ… ECBTP 109. 120. 123. 130. 132. 130. 123.
# ℹ 20 more variables: `2002` <dbl>, `2003` <dbl>, `2004` <dbl>, `2005` <dbl>,
# `2006` <dbl>, `2007` <dbl>, `2008` <dbl>, `2009` <dbl>, `2010` <dbl>,
# `2011` <dbl>, `2012` <dbl>, `2013` <dbl>, `2014` <dbl>, `2015` <dbl>,
# `2016` <dbl>, `2017` <dbl>, `2018` <dbl>, `2019` <dbl>, `2020` <dbl>,
# `2021` <dbl>
Now that this data is more friendly, I want to pivot this data longer to collapse the years into rows. We will also sort the data based on year, then country, then customize the order by Indicator for a more intuitive numerical view.
co2_longer <- co2_emissions_tidy %>%
# Pivot year columns to rows
pivot_longer(cols = 4:30 , names_to = "year", values_to = "metric_tons_millions") %>%
# Sort by year, country, custom Indicator
arrange(year, Country, factor(Indicator, levels = c("CO2_production", "CO2_final_domestic_demand", "CO2_final_demand_balance", "CO2_gross_exports", "CO2_gross_imports", "CO2_gross_exports_balance"))) %>%
# Move year to first column
select(year, everything())
head(co2_longer)# A tibble: 6 × 5
year Country Indicator CTS_Code metric_tons_millions
<chr> <chr> <chr> <chr> <dbl>
1 1995 Argentina CO2_production ECBTP 109.
2 1995 Argentina CO2_final_domestic_demand ECBTDD 124.
3 1995 Argentina CO2_final_demand_balance ECBTDB -14.4
4 1995 Argentina CO2_gross_exports ECBTTX 9.47
5 1995 Argentina CO2_gross_imports ECBTT 23.5
6 1995 Argentina CO2_gross_exports_balance ECBTT -14.4
dim(co2_longer)[1] 10854 5
colnames(co2_longer)[1] "year" "Country" "Indicator"
[4] "CTS_Code" "metric_tons_millions"
This data set contains information about CO2 emissions from domestic demand, production and trade for 66 countries from 1995-2021 measured in millions of metric tons. For 2019-2021 there are only values for the export and import emissions with not all countries having data. There are 66 prominent countries, as well as ‘Rest of World’ listed.
unique(co2_longer$Country) [1] "Argentina" "Australia"
[3] "Austria" "Belgium"
[5] "Brazil" "Brunei Darussalam"
[7] "Bulgaria" "Cambodia"
[9] "Canada" "Chile"
[11] "China, P.R.: Hong Kong" "China, P.R.: Mainland"
[13] "Colombia" "Costa Rica"
[15] "Croatia, Rep. of" "Cyprus"
[17] "Czech Rep." "Denmark"
[19] "Estonia, Rep. of" "Finland"
[21] "France" "Germany"
[23] "Greece" "Hungary"
[25] "Iceland" "India"
[27] "Indonesia" "Ireland"
[29] "Israel" "Italy"
[31] "Japan" "Kazakhstan, Rep. of"
[33] "Korea, Rep. of" "Lao People's Dem. Rep."
[35] "Latvia" "Lithuania"
[37] "Luxembourg" "Malaysia"
[39] "Malta" "Mexico"
[41] "Morocco" "Myanmar"
[43] "Netherlands, The" "New Zealand"
[45] "Norway" "Peru"
[47] "Philippines" "Poland, Rep. of"
[49] "Portugal" "Rest of the World"
[51] "Romania" "Russian Federation"
[53] "Saudi Arabia" "Singapore"
[55] "Slovak Rep." "Slovenia, Rep. of"
[57] "South Africa" "Spain"
[59] "Sweden" "Switzerland"
[61] "Taiwan Province of China" "Thailand"
[63] "Tunisia" "Turkey"
[65] "United Kingdom" "United States"
[67] "Vietnam"
tail(co2_longer)# A tibble: 6 × 5
year Country Indicator CTS_Code metric_tons_millions
<chr> <chr> <chr> <chr> <dbl>
1 2021 Vietnam CO2_production ECBTP NA
2 2021 Vietnam CO2_final_domestic_demand ECBTDD NA
3 2021 Vietnam CO2_final_demand_balance ECBTDB NA
4 2021 Vietnam CO2_gross_exports ECBTTX 420.
5 2021 Vietnam CO2_gross_imports ECBTT 227.
6 2021 Vietnam CO2_gross_exports_balance ECBTT 193.
The Indicator and CTS_Code seems to be almost interchangeable. The (renamed) Indicator values represent CO2 Emissions embodied in production, CO2 Emissions embodied in final domestic demand, the balance of CO2 Emissions embodied in final demand, CO2 Emissions embodied in gross exports, CO2 Emissions embodied in gross imports, and the balance of CO2 Emissions embodied in gross exports. To summarize, this data tracks how much CO2 is emitted by, consumed by, and embodied in trade between different countries. Negative values for demand balance and gross exports balance indicate the country’s demand is higher than its production and its imports are greater than its exports, respectively.
unique(co2_longer$Indicator)[1] "CO2_production" "CO2_final_domestic_demand"
[3] "CO2_final_demand_balance" "CO2_gross_exports"
[5] "CO2_gross_imports" "CO2_gross_exports_balance"
Compute descriptive statistics on the variables of interest (i.e., mean, median, sd for numerical variables, counts and proportions for categorical variables)
We can group the data by year and find the mean, median, max and standard deviation for CO2 emissions embodied in production.
co2_longer %>%
filter(Indicator == "CO2_production") %>%
group_by(year) %>%
summarise(mean = mean(metric_tons_millions), median=median(metric_tons_millions), max=max(metric_tons_millions), sd=sd(metric_tons_millions))# A tibble: 27 × 5
year mean median max sd
<chr> <dbl> <dbl> <dbl> <dbl>
1 1995 319. 62.2 5153. 773.
2 1996 326. 67.3 5310. 786.
3 1997 332. 72.4 5580. 815.
4 1998 334. 70.1 5626. 824.
5 1999 336. 69.6 5648. 824.
6 2000 347. 69.9 5820. 852.
7 2001 352. 69.8 5779. 859.
8 2002 357. 70.0 5631. 862.
9 2003 372. 75.6 5696. 904.
10 2004 390. 77.7 5803. 962.
# ℹ 17 more rows
We can also find the greatest emitters and consumers of CO2 emissions of all time.
co2_longer %>%
filter(Indicator == "CO2_production")%>%
arrange(desc(metric_tons_millions)) %>%
slice(1:5)# A tibble: 5 × 5
year Country Indicator CTS_Code metric_tons_millions
<chr> <chr> <chr> <chr> <dbl>
1 2018 China, P.R.: Mainland CO2_production ECBTP 9916.
2 2017 China, P.R.: Mainland CO2_production ECBTP 9503.
3 2014 China, P.R.: Mainland CO2_production ECBTP 9302.
4 2013 China, P.R.: Mainland CO2_production ECBTP 9298.
5 2016 China, P.R.: Mainland CO2_production ECBTP 9277.
co2_longer %>%
filter(Indicator == "CO2_final_domestic_demand")%>%
arrange(desc(metric_tons_millions)) %>%
slice(1:5)# A tibble: 5 × 5
year Country Indicator CTS_Code metric_tons_millions
<chr> <chr> <chr> <chr> <dbl>
1 2018 China, P.R.: Mainland CO2_final_domestic_… ECBTDD 9020.
2 2017 China, P.R.: Mainland CO2_final_domestic_… ECBTDD 8558.
3 2016 China, P.R.: Mainland CO2_final_domestic_… ECBTDD 8407.
4 2015 China, P.R.: Mainland CO2_final_domestic_… ECBTDD 8322.
5 2014 China, P.R.: Mainland CO2_final_domestic_… ECBTDD 8231.
We can also find countries that are below the median for consuming products of CO2 emissions.
co2_longer %>%
filter(year == 2018 & Indicator == "CO2_final_domestic_demand")%>%
group_by(year) %>%
filter(metric_tons_millions < median(metric_tons_millions)) %>%
arrange(metric_tons_millions)# A tibble: 33 × 5
# Groups: year [1]
year Country Indicator CTS_Code metric_tons_millions
<chr> <chr> <chr> <chr> <dbl>
1 2018 Malta CO2_final_domesti… ECBTDD 4.16
2 2018 Iceland CO2_final_domesti… ECBTDD 4.34
3 2018 Brunei Darussalam CO2_final_domesti… ECBTDD 8.57
4 2018 Cyprus CO2_final_domesti… ECBTDD 9.08
5 2018 Latvia CO2_final_domesti… ECBTDD 10.5
6 2018 Lao People's Dem. Rep. CO2_final_domesti… ECBTDD 11.9
7 2018 Luxembourg CO2_final_domesti… ECBTDD 13.0
8 2018 Costa Rica CO2_final_domesti… ECBTDD 13.9
9 2018 Estonia, Rep. of CO2_final_domesti… ECBTDD 14.3
10 2018 Cambodia CO2_final_domesti… ECBTDD 14.9
# ℹ 23 more rows
There are a number of potential research questions this data set can help answer.
Which countries are the largest producers and consumers of CO2 emissions?
Have global regulations or other factors influenced countries to reduce their production and consumption of CO2 emissions in more recent years?
Are there any patterns indicating that global CO2 emissions are increasing or decreasing?
Are more countries primarily importing or exporting goods that embody more CO2 emissions?
Is there a relationship between a country’s import/export emissions compared to its production and/or demand?
Although not included in this data, it would be great to see how population size plays into CO2 emissions and if that changes emission rankings at all.