Homework 6

Load packages

options(repos = c(CRAN = "https://cran.rstudio.com/"))
usethis::use_blank_slate()
# Load the necessary packages
install.packages("tidyverse")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//RtmpWgy5fy/downloaded_packages
install.packages("countrycode")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//RtmpWgy5fy/downloaded_packages
install.packages("janitor")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//RtmpWgy5fy/downloaded_packages
install.packages("readxl")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//RtmpWgy5fy/downloaded_packages
install.packages("here")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//RtmpWgy5fy/downloaded_packages
library(here)
here() starts at /Users/palesamnguni/Desktop/Courses/Spring Term/Sustainable Fin/Sus Finance HW/Sus_Finance_Homework/Homework6
library(readxl)
library(tidyr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(countrycode)
library(ggplot2)
library(dplyr)
library(ggplot2)
library(janitor) 

Attaching package: 'janitor'
The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(snakecase)
library(tidyverse) # because, always
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ lubridate 1.9.3     ✔ stringr   1.5.1
✔ purrr     1.0.2     ✔ tibble    3.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(stringr)
options(scipen = 999)

Scope 1 data: UNFCC

# UNFCC (scope 1)
ghg_emissions <- read_excel("ghg-emissions.xlsx", range = "A1:AF196")
Warning: Coercing boolean to numeric in D122 / R122C4
head(ghg_emissions)
# A tibble: 6 × 32
  iso   `Country/Region` unit     `1990`  `1991`  `1992`  `1993`  `1994`  `1995`
  <chr> <chr>            <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 OECD  OECD             MtCO2e 13443.   1.35e+4 1.35e+4 1.36e+4 1.38e+4 1.40e+4
2 WORLD World            MtCO2e 32661.   3.28e+4 3.27e+4 3.29e+4 3.32e+4 3.40e+4
3 AFG   Afghanistan      MtCO2e     9.24 9.51e+0 9.16e+0 9.29e+0 9.34e+0 9.79e+0
4 ALB   Albania          MtCO2e    11.4  9.01e+0 6.97e+0 6.94e+0 7.75e+0 7.59e+0
5 DZA   Algeria          MtCO2e   123.   1.27e+2 1.27e+2 1.36e+2 1.38e+2 1.44e+2
6 AND   Andorra          MtCO2e     0.43 4.3 e-1 4.3 e-1 4.4 e-1 4.4 e-1 4.6 e-1
# ℹ 23 more variables: `1996` <dbl>, `1997` <dbl>, `1998` <dbl>, `1999` <dbl>,
#   `2000` <dbl>, `2001` <dbl>, `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>
# Remove the "iso" column
ghg_emissions <- ghg_emissions |>
  select(-iso,-unit)

# Rename the 'Country/Region' column to 'country'
ghg_emissions <- ghg_emissions |>
  rename(country = `Country/Region`)
head(ghg_emissions)
# A tibble: 6 × 30
  country       `1990`   `1991`   `1992`  `1993`  `1994`  `1995`  `1996`  `1997`
  <chr>          <dbl>    <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 OECD        13443.   13460.   13494.   1.36e+4 1.38e+4 1.40e+4 1.44e+4 1.47e+4
2 World       32661.   32836.   32736.   3.29e+4 3.32e+4 3.40e+4 3.44e+4 3.57e+4
3 Afghanistan     9.24     9.51     9.16 9.29e+0 9.34e+0 9.79e+0 1.08e+1 1.18e+1
4 Albania        11.4      9.01     6.97 6.94e+0 7.75e+0 7.59e+0 7.32e+0 6.59e+0
5 Algeria       123.     127.     127.   1.36e+2 1.38e+2 1.44e+2 1.48e+2 1.47e+2
6 Andorra         0.43     0.43     0.43 4.4 e-1 4.4 e-1 4.6 e-1 4.9 e-1 5.1 e-1
# ℹ 21 more variables: `1998` <dbl>, `1999` <dbl>, `2000` <dbl>, `2001` <dbl>,
#   `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>
# Convert the 'country' column values to snake case
ghg_emissions$country <- to_snake_case(ghg_emissions$country)

# Pivot the data to long format
ghg_emissions_tidy <- ghg_emissions |>
  pivot_longer(
    cols = -country, # Selects all columns except 'country' to pivot
    names_to = "year",
    values_to = "ghg_emissions"
  ) |>
  # Optionally, clean the column names
  clean_names()

# View the first few rows to verify the changes
head(ghg_emissions_tidy)
# A tibble: 6 × 3
  country year  ghg_emissions
  <chr>   <chr>         <dbl>
1 oecd    1990         13443.
2 oecd    1991         13460.
3 oecd    1992         13494.
4 oecd    1993         13582.
5 oecd    1994         13813.
6 oecd    1995         13992.
#Saving the dataset as csv
write_csv(ghg_emissions, here("ghg_emissions_tidy.csv"))

Scope 1 data: EDGAR

# Load the dataset
edgar_wide <- read_excel("EDGAR_CH4_1970_2022.xlsx", 
    sheet = "TOTALS BY COUNTRY", range = "A10:BF233")

# Rename and clean column names, and convert country names to snake case
edgar_tidy <- edgar_wide |>
  rename_with(~str_to_lower(gsub("[^[:alnum:]_]+", "_", .)), everything()) |> # Clean all column names
  rename(country = name) |>
  mutate(country = to_snake_case(country)) |>
  pivot_longer(
    cols = !all_of(c("ipcc_annex", "c_group_im24_sh", "country_code_a3", "country", "substance")), # Exclude the specified columns by their names
    names_to = "year",
    values_to = "emissions",
    names_prefix = "y_" # Remove 'y_' prefix from year columns
  ) |>
  mutate(
    year = as.numeric(str_replace(year, "x", "")), # Convert 'year' column to numeric, removing any 'x' prefixes
    ipcc_annex = to_snake_case(str_to_lower(ipcc_annex)), # Clean and convert ipcc_annex to snake case
    c_group_im24_sh = to_snake_case(str_to_lower(c_group_im24_sh)) # Clean and convert c_group_im24_sh to snake case
  )

# View the first few rows to verify the changes
head(edgar_tidy)
# A tibble: 6 × 7
  ipcc_annex  c_group_im24_sh  country_code_a3 country substance  year emissions
  <chr>       <chr>            <chr>           <chr>   <chr>     <dbl>     <dbl>
1 non_annex_i rest_central_am… ABW             aruba   CH4        1970     0.642
2 non_annex_i rest_central_am… ABW             aruba   CH4        1971     0.683
3 non_annex_i rest_central_am… ABW             aruba   CH4        1972     0.687
4 non_annex_i rest_central_am… ABW             aruba   CH4        1973     0.692
5 non_annex_i rest_central_am… ABW             aruba   CH4        1974     0.694
6 non_annex_i rest_central_am… ABW             aruba   CH4        1975     0.695
#Saving the dataset as csv
write_csv(edgar_tidy, here("edgar_tidy.csv"))

Scope 1 data: total exports of goods and services

# Co2 in total exports of goods and services (scope 2)
CO2_totexports <- read_excel("co2 exports goods and services.xls", 
    range = "A7:Z91")
New names:
• `` -> `...2`
head(CO2_totexports)
# A tibble: 6 × 26
  Time      ...2  `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003`
  <chr>     <lgl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Country   NA      NA     NA     NA     NA     NA     NA     NA     NA     NA  
2 OECD: OE… NA     604.   655.   715.   687.   659.   734.   747.   730.   759. 
3 AUS: Aus… NA      76.7   86.7   87.4   92.2   96.2  101.   107.    94.6   95.1
4 AUT: Aus… NA      30.3   31.5   34.1   34.8   35.2   38.6   40.9   42.0   44.5
5 BEL: Bel… NA      83.4   83.8   84.3   87.3   87.3   97.8   93.3   85.5   87.1
6 CAN: Can… NA     206.   217.   237.   252.   264.   287.   279.   267.   263. 
# ℹ 15 more variables: `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>
# Remove the first row which seems to be a redundant header and column
CO2_totexports <- CO2_totexports[-1, -2]

# Rename the 'Time' column to 'country'
CO2_totexports <- CO2_totexports |>
  rename(country = Time)

# This will remove any prefix that ends with a colon and a space
CO2_totexports$country <- sub(".*: ", "", CO2_totexports$country)

# Pivot the data to long format
CO2_totexports_long <- CO2_totexports |>
  pivot_longer(
    cols = -country, # This will select all columns except 'country' to pivot
    names_to = "year",
    values_to = "CO2_totexports"
  )

# Pivot the data back to wide format
CO2_totexports_wide <- CO2_totexports_long |>
  pivot_wider(
    names_from = year,
    values_from = CO2_totexports
  ) |>
  # Use snakecase to clean the country names
  mutate(country = to_snake_case(country))

# View the first few rows of the wide format data
head(CO2_totexports_wide)
# A tibble: 6 × 25
  country  `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
  <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 oecd_me…  604.   655.   715.   687.   659.   734.   747.   730.   759.   868. 
2 austral…   76.7   86.7   87.4   92.2   96.2  101.   107.    94.6   95.1   96.2
3 austria    30.3   31.5   34.1   34.8   35.2   38.6   40.9   42.0   44.5   49.2
4 belgium    83.4   83.8   84.3   87.3   87.3   97.8   93.3   85.5   87.1   91.6
5 canada    206.   217.   237.   252.   264.   287.   279.   267.   263.   270. 
6 chile      15.5   16.7   20.7   22.0   24.4   24.0   24.3   24.3   25.4   36.1
# ℹ 14 more variables: `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>
# Pivot back to long format as tidy data
CO2_totexports_tidy <- CO2_totexports_wide |>
  pivot_longer(
    cols = -country,
    names_to = "year",
    values_to = "CO2_totexports"
  )

# View the first few rows of the tidy data
head(CO2_totexports_tidy)
# A tibble: 6 × 3
  country               year  CO2_totexports
  <chr>                 <chr>          <dbl>
1 oecd_member_countries 1995            604.
2 oecd_member_countries 1996            655.
3 oecd_member_countries 1997            715.
4 oecd_member_countries 1998            687.
5 oecd_member_countries 1999            659.
6 oecd_member_countries 2000            734.
#Saving the dataset as csv
write_csv(CO2_totexports_tidy, here("CO2_totexports_tidy.csv"))

Scope 2 data

# Co2 in total imports of electricity, steam (scope 2)

#Load dataset
CO2_energyimports <- read_excel("Co2 imports electricity.xls", 
    sheet = "OECD.Stat export", range = "A7:Z91")
New names:
• `` -> `...2`
head(CO2_energyimports)
# A tibble: 6 × 26
  Time      ...2  `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003`
  <chr>     <lgl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Country   NA    NA     NA     NA     NA     NA     NA     NA     NA     NA    
2 OECD: OE… NA     0.03   0.033  0.039  0.042  0.04   0.039  0.045  0.051  0.06 
3 AUS: Aus… NA     0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001
4 AUT: Aus… NA     0.001  0.002  0.002  0.002  0.002  0.005  0.012  0.022  0.027
5 BEL: Bel… NA     0.002  0.003  0.003  0.002  0.002  0.005  0.009  0.014  0.019
6 CAN: Can… NA     0.023  0.026  0.042  0.053  0.042  0.05   0.081  0.05   0.069
# ℹ 15 more variables: `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>
# Remove the first row which seems to be a redundant header
CO2_energyimports <- CO2_energyimports[-1, -2]
CO2_energyimports
# A tibble: 83 × 25
   Time    `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
   <chr>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1 OECD: …  0.03   0.033  0.039  0.042  0.04   0.039  0.045  0.051  0.06   0.06 
 2 AUS: A…  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001
 3 AUT: A…  0.001  0.002  0.002  0.002  0.002  0.005  0.012  0.022  0.027  0.005
 4 BEL: B…  0.002  0.003  0.003  0.002  0.002  0.005  0.009  0.014  0.019  0.032
 5 CAN: C…  0.023  0.026  0.042  0.053  0.042  0.05   0.081  0.05   0.069  0.063
 6 CHL: C…  0      0      0      0      0      0      0      0      0      0    
 7 COL: C…  0      0      0      0      0      0      0      0      0      0    
 8 CRI: C…  0      0      0      0      0      0      0      0      0      0    
 9 CZE: C…  0.002  0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.011  0.008
10 DNK: D…  0      0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.002  0.004
# ℹ 73 more rows
# ℹ 14 more variables: `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>
# Rename the 'Time' column to 'country'
CO2_energyimports <- CO2_energyimports |>
  rename(country = Time)
CO2_energyimports
# A tibble: 83 × 25
   country `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
   <chr>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1 OECD: …  0.03   0.033  0.039  0.042  0.04   0.039  0.045  0.051  0.06   0.06 
 2 AUS: A…  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001
 3 AUT: A…  0.001  0.002  0.002  0.002  0.002  0.005  0.012  0.022  0.027  0.005
 4 BEL: B…  0.002  0.003  0.003  0.002  0.002  0.005  0.009  0.014  0.019  0.032
 5 CAN: C…  0.023  0.026  0.042  0.053  0.042  0.05   0.081  0.05   0.069  0.063
 6 CHL: C…  0      0      0      0      0      0      0      0      0      0    
 7 COL: C…  0      0      0      0      0      0      0      0      0      0    
 8 CRI: C…  0      0      0      0      0      0      0      0      0      0    
 9 CZE: C…  0.002  0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.011  0.008
10 DNK: D…  0      0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.002  0.004
# ℹ 73 more rows
# ℹ 14 more variables: `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>
# This will remove any prefix that ends with a colon and a space
CO2_energyimports$country <- sub(".*: ", "", CO2_energyimports$country)
CO2_energyimports
# A tibble: 83 × 25
   country `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
   <chr>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1 OECD m…  0.03   0.033  0.039  0.042  0.04   0.039  0.045  0.051  0.06   0.06 
 2 Austra…  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001
 3 Austria  0.001  0.002  0.002  0.002  0.002  0.005  0.012  0.022  0.027  0.005
 4 Belgium  0.002  0.003  0.003  0.002  0.002  0.005  0.009  0.014  0.019  0.032
 5 Canada   0.023  0.026  0.042  0.053  0.042  0.05   0.081  0.05   0.069  0.063
 6 Chile    0      0      0      0      0      0      0      0      0      0    
 7 Colomb…  0      0      0      0      0      0      0      0      0      0    
 8 Costa …  0      0      0      0      0      0      0      0      0      0    
 9 Czechia  0.002  0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.011  0.008
10 Denmark  0      0.002  0.001  0.001  0.001  0.001  0.001  0.003  0.002  0.004
# ℹ 73 more rows
# ℹ 14 more variables: `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>
# Now pivot the data to long format
CO2_energyimports_long <- CO2_energyimports |>
  pivot_longer(
    cols = -country, # This will select all columns except 'country' to pivot
    names_to = "year",
    values_to = "CO2_energyimports"
  ) 

CO2_energyimports_long
# A tibble: 1,992 × 3
   country               year  CO2_energyimports
   <chr>                 <chr>             <dbl>
 1 OECD member countries 1995              0.03 
 2 OECD member countries 1996              0.033
 3 OECD member countries 1997              0.039
 4 OECD member countries 1998              0.042
 5 OECD member countries 1999              0.04 
 6 OECD member countries 2000              0.039
 7 OECD member countries 2001              0.045
 8 OECD member countries 2002              0.051
 9 OECD member countries 2003              0.06 
10 OECD member countries 2004              0.06 
# ℹ 1,982 more rows
# Pivot the data from long back to wide format
CO2_energyimports_wide <- CO2_energyimports_long |>
  pivot_wider(
    names_from = year,
    values_from = CO2_energyimports
  ) |>
  # Use snakecase to clean the country names
  mutate(country = to_snake_case(country)) 

head (CO2_energyimports_wide)
# A tibble: 6 × 25
  country  `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
  <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 oecd_me…  0.03   0.033  0.039  0.042  0.04   0.039  0.045  0.051  0.06   0.06 
2 austral…  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001  0.001
3 austria   0.001  0.002  0.002  0.002  0.002  0.005  0.012  0.022  0.027  0.005
4 belgium   0.002  0.003  0.003  0.002  0.002  0.005  0.009  0.014  0.019  0.032
5 canada    0.023  0.026  0.042  0.053  0.042  0.05   0.081  0.05   0.069  0.063
6 chile     0      0      0      0      0      0      0      0      0      0    
# ℹ 14 more variables: `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>
#Pivot long as tidy data

CO2_energyimports_tidy <- CO2_energyimports_wide |>
  pivot_longer(cols = -country,
    names_to = "year",
    values_to = "CO2_energyimports"
  )

head(CO2_energyimports_tidy)
# A tibble: 6 × 3
  country               year  CO2_energyimports
  <chr>                 <chr>             <dbl>
1 oecd_member_countries 1995              0.03 
2 oecd_member_countries 1996              0.033
3 oecd_member_countries 1997              0.039
4 oecd_member_countries 1998              0.042
5 oecd_member_countries 1999              0.04 
6 oecd_member_countries 2000              0.039
write_csv(CO2_energyimports_tidy, here("CO2_energyimports_tidy.csv"))

Scope 3

# Load dataset
CO2_totalimports <- read_excel("co2 total imports.xlsx", 
    sheet = "OECD.Stat export", range = "A7:Z91")
New names:
• `` -> `...2`
head(CO2_totalimports)
# A tibble: 6 × 26
  Time  ...2  `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
  <chr> <lgl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 Coun… NA    NA     NA     NA     NA     NA      NA      NA      NA      NA    
2 OECD… NA    72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
3 AUS:… NA     0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
4 AUT:… NA     0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
5 BEL:… NA     0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
6 CAN:… NA     2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
# ℹ 15 more variables: `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>
#CO2 non-energy = total imports - energy imports. Need to tidy total imports data first
# Remove the first row which seems to be a redundant header and column
CO2_totalimports<- CO2_totalimports[-1, -2]
CO2_totalimports
# A tibble: 83 × 25
   Time       `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
   <chr>       <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 OECD: OEC… 72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
 2 AUS: Aust…  0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
 3 AUT: Aust…  0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
 4 BEL: Belg…  0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
 5 CAN: Cana…  2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
 6 CHL: Chile  0.016  0.017  0.025  0.029  0.03    0.033   0.034   0.035   0.035
 7 COL: Colo…  0.016  0.016  0.019  0.018  0.013   0.014   0.014   0.014   0.014
 8 CRI: Cost…  0      0      0      0.001  0.001   0       0       0       0    
 9 CZE: Czec…  0.588  0.494  0.427  0.301  0.222   0.261   0.265   0.268   0.308
10 DNK: Denm…  0.134  0.215  0.234  0.158  0.206   0.232   0.233   0.243   0.284
# ℹ 73 more rows
# ℹ 15 more variables: `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>
# Rename the 'Time' column to 'country'
CO2_totalimports<- CO2_totalimports |>
  rename(country = Time)
CO2_totalimports
# A tibble: 83 × 25
   country    `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
   <chr>       <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 OECD: OEC… 72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
 2 AUS: Aust…  0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
 3 AUT: Aust…  0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
 4 BEL: Belg…  0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
 5 CAN: Cana…  2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
 6 CHL: Chile  0.016  0.017  0.025  0.029  0.03    0.033   0.034   0.035   0.035
 7 COL: Colo…  0.016  0.016  0.019  0.018  0.013   0.014   0.014   0.014   0.014
 8 CRI: Cost…  0      0      0      0.001  0.001   0       0       0       0    
 9 CZE: Czec…  0.588  0.494  0.427  0.301  0.222   0.261   0.265   0.268   0.308
10 DNK: Denm…  0.134  0.215  0.234  0.158  0.206   0.232   0.233   0.243   0.284
# ℹ 73 more rows
# ℹ 15 more variables: `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>
# This will remove any prefix that ends with a colon and a space
CO2_totalimports$country <- sub(".*: ", "", CO2_totalimports$country)
CO2_totalimports
# A tibble: 83 × 25
   country    `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
   <chr>       <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 OECD memb… 72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
 2 Australia   0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
 3 Austria     0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
 4 Belgium     0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
 5 Canada      2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
 6 Chile       0.016  0.017  0.025  0.029  0.03    0.033   0.034   0.035   0.035
 7 Colombia    0.016  0.016  0.019  0.018  0.013   0.014   0.014   0.014   0.014
 8 Costa Rica  0      0      0      0.001  0.001   0       0       0       0    
 9 Czechia     0.588  0.494  0.427  0.301  0.222   0.261   0.265   0.268   0.308
10 Denmark     0.134  0.215  0.234  0.158  0.206   0.232   0.233   0.243   0.284
# ℹ 73 more rows
# ℹ 15 more variables: `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>
CO2_totalimports
# A tibble: 83 × 25
   country    `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
   <chr>       <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 OECD memb… 72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
 2 Australia   0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
 3 Austria     0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
 4 Belgium     0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
 5 Canada      2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
 6 Chile       0.016  0.017  0.025  0.029  0.03    0.033   0.034   0.035   0.035
 7 Colombia    0.016  0.016  0.019  0.018  0.013   0.014   0.014   0.014   0.014
 8 Costa Rica  0      0      0      0.001  0.001   0       0       0       0    
 9 Czechia     0.588  0.494  0.427  0.301  0.222   0.261   0.265   0.268   0.308
10 Denmark     0.134  0.215  0.234  0.158  0.206   0.232   0.233   0.243   0.284
# ℹ 73 more rows
# ℹ 15 more variables: `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>
# Now pivot the data to long format
CO2_totalimports_long <- CO2_totalimports |>
  pivot_longer(
    cols = -country, # This will select all columns except 'country' to pivot
    names_to = "year",
    values_to = "CO2_totalimports"
  ) 

CO2_totalimports_long
# A tibble: 1,992 × 3
   country               year  CO2_totalimports
   <chr>                 <chr>            <dbl>
 1 OECD member countries 1995              72.8
 2 OECD member countries 1996              80.9
 3 OECD member countries 1997              87.0
 4 OECD member countries 1998              89.1
 5 OECD member countries 1999              93.3
 6 OECD member countries 2000             111. 
 7 OECD member countries 2001             108. 
 8 OECD member countries 2002             105. 
 9 OECD member countries 2003             112. 
10 OECD member countries 2004             128. 
# ℹ 1,982 more rows
# Pivot the data from long back to wide format
CO2_totalimports_wide <- CO2_totalimports_long |>
  pivot_wider(
    names_from = year,
    values_from = CO2_totalimports
  ) |>
  # Use snakecase to clean the country names
  mutate(country = to_snake_case(country)) 

head (CO2_totalimports_wide)
# A tibble: 6 × 25
  country     `1995` `1996` `1997` `1998` `1999`  `2000`  `2001`  `2002`  `2003`
  <chr>        <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 oecd_membe… 72.8   80.9   87.0   89.1   93.3   111.    108.    105.    112.   
2 australia    0.492  0.593  0.636  0.669  0.746   0.75    0.799   0.694   0.687
3 austria      0.184  0.201  0.212  0.219  0.218   0.256   0.277   0.286   0.307
4 belgium      0.66   0.666  0.63   0.645  0.647   0.739   0.719   0.65    0.678
5 canada       2.62   2.71   3.18   3.31   3.36    3.67    3.35    3.23    3.17 
6 chile        0.016  0.017  0.025  0.029  0.03    0.033   0.034   0.035   0.035
# ℹ 15 more variables: `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>
#Pivot long as tidy data

CO2_totalimports_tidy <- CO2_totalimports_wide |>
  pivot_longer(cols = -country,
    names_to = "year",
    values_to = "CO2_totalimports"
  )

CO2_totalimports_tidy
# A tibble: 1,992 × 3
   country               year  CO2_totalimports
   <chr>                 <chr>            <dbl>
 1 oecd_member_countries 1995              72.8
 2 oecd_member_countries 1996              80.9
 3 oecd_member_countries 1997              87.0
 4 oecd_member_countries 1998              89.1
 5 oecd_member_countries 1999              93.3
 6 oecd_member_countries 2000             111. 
 7 oecd_member_countries 2001             108. 
 8 oecd_member_countries 2002             105. 
 9 oecd_member_countries 2003             112. 
10 oecd_member_countries 2004             128. 
# ℹ 1,982 more rows
# Check column names for both dataframes
colnames(CO2_totalimports_tidy)
[1] "country"          "year"             "CO2_totalimports"
colnames(CO2_energyimports_tidy)
[1] "country"           "year"              "CO2_energyimports"
# Join the CO2 total imports and energy imports data frames using the correct column names
CO2_totalimports_joined <- CO2_totalimports_tidy |>
  left_join(CO2_energyimports_tidy, by = c("country", "year"))

# View the first few rows to verify the changes
head(CO2_totalimports_joined)
# A tibble: 6 × 4
  country               year  CO2_totalimports CO2_energyimports
  <chr>                 <chr>            <dbl>             <dbl>
1 oecd_member_countries 1995              72.8             0.03 
2 oecd_member_countries 1996              80.9             0.033
3 oecd_member_countries 1997              87.0             0.039
4 oecd_member_countries 1998              89.1             0.042
5 oecd_member_countries 1999              93.3             0.04 
6 oecd_member_countries 2000             111.              0.039
#Now to calculate non-energy imports
CO2_totalimports_tidy<- CO2_totalimports_joined|>
      mutate(CO2_nonenergyimports=CO2_totalimports-CO2_energyimports)

CO2_totalimports_tidy
# A tibble: 1,992 × 5
   country         year  CO2_totalimports CO2_energyimports CO2_nonenergyimports
   <chr>           <chr>            <dbl>             <dbl>                <dbl>
 1 oecd_member_co… 1995              72.8             0.03                  72.8
 2 oecd_member_co… 1996              80.9             0.033                 80.9
 3 oecd_member_co… 1997              87.0             0.039                 87.0
 4 oecd_member_co… 1998              89.1             0.042                 89.0
 5 oecd_member_co… 1999              93.3             0.04                  93.2
 6 oecd_member_co… 2000             111.              0.039                111. 
 7 oecd_member_co… 2001             108.              0.045                108. 
 8 oecd_member_co… 2002             105.              0.051                105. 
 9 oecd_member_co… 2003             112.              0.06                 112. 
10 oecd_member_co… 2004             128.              0.06                 128. 
# ℹ 1,982 more rows
write_csv(CO2_totalimports_tidy, here("CO2_totalimports_tidy.csv"))

Other data

PPP adjusted GDP

#PPP-adjusted GDP


# Read in the data, clean column names, and convert country names to snake case
PPP_adjusted_gdp <- read_excel("API_NY.GDP.MKTP.PP.CD_DS2_en_excel_v2_205.xls", 
                               sheet = "Data", range = "A4:BO270") |>
                    clean_names() |>
                    mutate(country_name = to_snake_case(country_name))

# Pivot data to long format
PPP_adjusted_gdp_long <- PPP_adjusted_gdp |>
  pivot_longer(
    cols = !c(country_name, country_code, indicator_name, indicator_code),
    names_to = "year", 
    values_to = "gdp"
  )


# Remove the 'x' from the year column and convert it to integer
PPP_adjusted_gdp_long <- PPP_adjusted_gdp_long |>
  mutate(year = as.integer(str_remove(year, "x")))

# View the first few rows to verify the changes
head(PPP_adjusted_gdp_long)
# A tibble: 6 × 6
  country_name country_code indicator_name            indicator_code  year   gdp
  <chr>        <chr>        <chr>                     <chr>          <int> <dbl>
1 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1960    NA
2 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1961    NA
3 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1962    NA
4 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1963    NA
5 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1964    NA
6 aruba        ABW          GDP, PPP (current intern… NY.GDP.MKTP.P…  1965    NA
# Remove rows with NA in the GDP column
PPP_adjusted_gdp_long <- PPP_adjusted_gdp_long |>
  drop_na(gdp)
PPP_adjusted_gdp_long
# A tibble: 7,730 × 6
   country_name country_code indicator_name          indicator_code  year    gdp
   <chr>        <chr>        <chr>                   <chr>          <int>  <dbl>
 1 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1990 1.36e9
 2 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1991 1.52e9
 3 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1992 1.65e9
 4 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1993 1.81e9
 5 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1994 2.00e9
 6 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1995 2.10e9
 7 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1996 2.16e9
 8 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1997 2.35e9
 9 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1998 2.42e9
10 aruba        ABW          GDP, PPP (current inte… NY.GDP.MKTP.P…  1999 2.49e9
# ℹ 7,720 more rows
# Convert the year column to integer and round GDP values to two decimal places
PPP_adjusted_gdp_tidy <- PPP_adjusted_gdp_long |>
  mutate(
    year = as.integer(year), 
    gdp = round(as.numeric(gdp), 2)
  )

head(PPP_adjusted_gdp_tidy)
# A tibble: 6 × 6
  country_name country_code indicator_name           indicator_code  year    gdp
  <chr>        <chr>        <chr>                    <chr>          <int>  <dbl>
1 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1990 1.36e9
2 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1991 1.52e9
3 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1992 1.65e9
4 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1993 1.81e9
5 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1994 2.00e9
6 aruba        ABW          GDP, PPP (current inter… NY.GDP.MKTP.P…  1995 2.10e9
write_csv(PPP_adjusted_gdp_tidy, here("PPP_adjusted_gdp_tidy.csv"))

Nominal GDP

# Read in the data
nominal_gdp <- read_excel("imf-dm-export-20240302.xls", sheet = "NGDPD")

# Replace "no data" with NA, remove rows with any NAs, and round numbers
nominal_gdp <- nominal_gdp |>
  mutate(across(everything(), ~na_if(., "no data"))) |>
  drop_na() |>
  mutate(across(where(is.numeric), ~round(., 2)))

# Add a name to the first column if it doesn't have one
names(nominal_gdp)[1] <- "country_name"

# Clean the column names
nominal_gdp <- nominal_gdp |>
  clean_names()

# Pivot the data to long format
nominal_gdp_long <- nominal_gdp |>
  pivot_longer(
    cols = -country_name, # Excludes the first column which is now named 'country_name'
    names_to = "year",
    values_to = "gdp"
  )

# Remove the 'x' from the year column
nominal_gdp_long <- nominal_gdp_long |>
  mutate(year = str_remove(year, "x")) |>
  mutate(year = as.numeric(year)) # Convert year to numeric

# Pivot the data back to wide format
nominal_gdp_wide <- nominal_gdp_long |>
  pivot_wider(
    names_from = year,
    values_from = gdp
  )
nominal_gdp_wide
# A tibble: 171 × 50
   country_name   `1980` `1981` `1982` `1983` `1984` `1985` `1986` `1987` `1988`
   <chr>          <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr> 
 1 Albania        1.946  2.229… 2.295… 2.319  2.29   2.339  2.587… 2.565… 2.529…
 2 Algeria        42.34… 44.372 44.78… 47.52… 51.51… 61.13… 61.53… 63.29… 51.66…
 3 Angola         6.639… 6.214… 6.214… 6.476  6.863… 8.457… 7.918… 9.050… 9.817…
 4 Antigua and B… 0.131… 0.147… 0.164… 0.182  0.207… 0.240… 0.289… 0.337… 0.399…
 5 Argentina      233.6… 189.8… 94.25  116.2… 130.5… 98.59… 118.5… 121.5… 142.3…
 6 Australia      162.9… 188.4… 187.0… 179.5… 197.1… 174.4… 181.5… 213.1… 271.1…
 7 Austria        80.92… 70.12… 70.11… 71.03… 67.00… 68.62… 97.375 121.7… 133.5…
 8 Bahamas, The   2.597… 2.692… 3.031… 3.373… 3.603… 3.923  4.240… 4.668… 4.855…
 9 Bahrain        3.593  4.056  4.264… 4.368… 4.533… 4.277… 3.347  3.625… 4.480…
10 Bangladesh     24.77… 26.355 24.67… 23.904 26.817 28.81… 29.63… 32.44… 34.91…
# ℹ 161 more rows
# ℹ 40 more variables: `1989` <chr>, `1990` <chr>, `1991` <chr>, `1992` <chr>,
#   `1993` <chr>, `1994` <chr>, `1995` <chr>, `1996` <chr>, `1997` <chr>,
#   `1998` <chr>, `1999` <chr>, `2000` <chr>, `2001` <chr>, `2002` <chr>,
#   `2003` <chr>, `2004` <chr>, `2005` <chr>, `2006` <chr>, `2007` <chr>,
#   `2008` <chr>, `2009` <chr>, `2010` <chr>, `2011` <chr>, `2012` <chr>,
#   `2013` <chr>, `2014` <chr>, `2015` <chr>, `2016` <chr>, `2017` <chr>, …
# Convert country names into snake_case
nominal_gdp_wide <- nominal_gdp_wide |>
  mutate(country_name = to_snake_case(country_name))
nominal_gdp_wide
# A tibble: 171 × 50
   country_name   `1980` `1981` `1982` `1983` `1984` `1985` `1986` `1987` `1988`
   <chr>          <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr> 
 1 albania        1.946  2.229… 2.295… 2.319  2.29   2.339  2.587… 2.565… 2.529…
 2 algeria        42.34… 44.372 44.78… 47.52… 51.51… 61.13… 61.53… 63.29… 51.66…
 3 angola         6.639… 6.214… 6.214… 6.476  6.863… 8.457… 7.918… 9.050… 9.817…
 4 antigua_and_b… 0.131… 0.147… 0.164… 0.182  0.207… 0.240… 0.289… 0.337… 0.399…
 5 argentina      233.6… 189.8… 94.25  116.2… 130.5… 98.59… 118.5… 121.5… 142.3…
 6 australia      162.9… 188.4… 187.0… 179.5… 197.1… 174.4… 181.5… 213.1… 271.1…
 7 austria        80.92… 70.12… 70.11… 71.03… 67.00… 68.62… 97.375 121.7… 133.5…
 8 bahamas_the    2.597… 2.692… 3.031… 3.373… 3.603… 3.923  4.240… 4.668… 4.855…
 9 bahrain        3.593  4.056  4.264… 4.368… 4.533… 4.277… 3.347  3.625… 4.480…
10 bangladesh     24.77… 26.355 24.67… 23.904 26.817 28.81… 29.63… 32.44… 34.91…
# ℹ 161 more rows
# ℹ 40 more variables: `1989` <chr>, `1990` <chr>, `1991` <chr>, `1992` <chr>,
#   `1993` <chr>, `1994` <chr>, `1995` <chr>, `1996` <chr>, `1997` <chr>,
#   `1998` <chr>, `1999` <chr>, `2000` <chr>, `2001` <chr>, `2002` <chr>,
#   `2003` <chr>, `2004` <chr>, `2005` <chr>, `2006` <chr>, `2007` <chr>,
#   `2008` <chr>, `2009` <chr>, `2010` <chr>, `2011` <chr>, `2012` <chr>,
#   `2013` <chr>, `2014` <chr>, `2015` <chr>, `2016` <chr>, `2017` <chr>, …
#Pivot the tidy data back to long format
nominal_gdp_long_tidy<-nominal_gdp_wide|> pivot_longer(
    cols = -country_name, # Excludes the first column which is now named 'country_name'
    names_to = "year",
    values_to = "gdp"
  )
  
head(nominal_gdp_long_tidy)
# A tibble: 6 × 3
  country_name year  gdp               
  <chr>        <chr> <chr>             
1 albania      1980  1.946             
2 albania      1981  2.2290000000000001
3 albania      1982  2.2959999999999998
4 albania      1983  2.319             
5 albania      1984  2.29              
6 albania      1985  2.339             
write_csv(nominal_gdp_long_tidy, here("nominal_gdp_long_tidy.csv"))

Population

#Population 
poulation <- read_excel("API_SP.POP.TOTL_DS2_en_excel_v2_218.xls", 
    sheet = "Data", range = "A4:BO270")

#Pivot data wide to tidy country names
population_wide <- population |>
pivot_wider(
    names_from = year,
    values_from = population
  )|> mutate(country = to_snake_case(country))

head(population_wide)
# A tibble: 6 × 20
  country  `1995` `1996` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004`
  <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 afghani… 1.76e7 1.84e7 1.90e7 1.95e7 2.00e7 2.06e7 2.13e7 2.22e7 2.31e7 2.40e7
2 albania  3.36e6 3.34e6 3.33e6 3.33e6 3.32e6 3.30e6 3.29e6 3.26e6 3.24e6 3.22e6
3 algeria  2.93e7 2.98e7 3.03e7 3.08e7 3.13e7 3.17e7 3.22e7 3.26e7 3.30e7 3.35e7
4 america… 5.29e4 5.39e4 5.49e4 5.59e4 5.68e4 5.75e4 5.82e4 5.87e4 5.91e4 5.93e4
5 andorra  6.39e4 6.43e4 6.41e4 6.38e4 6.41e4 6.54e4 6.80e4 7.16e4 7.56e4 7.91e4
6 angola   1.21e7 1.25e7 1.28e7 1.31e7 1.35e7 1.39e7 1.44e7 1.49e7 1.54e7 1.60e7
# ℹ 9 more variables: `2005` <dbl>, `2006` <dbl>, `2007` <dbl>, `2008` <dbl>,
#   `2009` <dbl>, `2010` <dbl>, `2011` <dbl>, `2012` <dbl>, `2013` <dbl>
#pivot data back to long format

population_long_tidy<- population_wide|>
  pivot_longer(cols = -country, # This selects all columns except the country for pivoting
    names_to = "year", 
              values_to= "population")

head(population_long_tidy)
# A tibble: 6 × 3
  country     year  population
  <chr>       <chr>      <dbl>
1 afghanistan 1995    17586073
2 afghanistan 1996    18415307
3 afghanistan 1997    19021226
4 afghanistan 1998    19496836
5 afghanistan 1999    19987071
6 afghanistan 2000    20595360
write_csv(population_long_tidy, here("population_long_tidy.csv"))