HW6

Data Source: https://www.climatewatchdata.org/ghg-emissions?end_year=2018&start_year=1990

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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(countrycode)
library(readxl)
library(dplyr)
library(here)
here() starts at /Users/hahazhou/Desktop/Critical Minerals
here("data-raw", "historical_emissions.xlsx")
[1] "/Users/hahazhou/Desktop/Critical Minerals/data-raw/historical_emissions.xlsx"
emissions <- read_excel(
  "data-raw/historical_emissions.xlsx",skip = 1)
emissions
# A tibble: 195 × 37
   ISO   Country    `Data source` Sector Gas   Unit  `2020` `2019` `2018` `2017`
   <chr> <chr>      <chr>         <chr>  <chr> <chr>  <dbl>  <dbl>  <dbl>  <dbl>
 1 WORLD World      Climate Watch Total… All … MtCO… 47513. 49881. 49586. 48391.
 2 CHN   China      Climate Watch Total… All … MtCO… 12296. 12085. 11877. 11367.
 3 USA   United St… Climate Watch Total… All … MtCO…  5289.  5819.  5936.  5729.
 4 IND   India      Climate Watch Total… All … MtCO…  3167.  3380.  3407.  3241.
 5 EUU   European … Climate Watch Total… All … MtCO…  2957.  3236.  3384.  3466.
 6 RUS   Russia     Climate Watch Total… All … MtCO…  1800.  1890.  1843.  1750.
 7 IDN   Indonesia  Climate Watch Total… All … MtCO…  1476.  1914.  1641.  1395.
 8 BRA   Brazil     Climate Watch Total… All … MtCO…  1470.  1467.  1443.  1486.
 9 JPN   Japan      Climate Watch Total… All … MtCO…  1063.  1126.  1166.  1210.
10 IRN   Iran       Climate Watch Total… All … MtCO…   845.   857.   892.   879.
# ℹ 185 more rows
# ℹ 27 more variables: `2016` <dbl>, `2015` <dbl>, `2014` <dbl>, `2013` <dbl>,
#   `2012` <dbl>, `2011` <dbl>, `2010` <dbl>, `2009` <dbl>, `2008` <dbl>,
#   `2007` <dbl>, `2006` <dbl>, `2005` <dbl>, `2004` <dbl>, `2003` <dbl>,
#   `2002` <dbl>, `2001` <dbl>, `2000` <dbl>, `1999` <dbl>, `1998` <dbl>,
#   `1997` <dbl>, `1996` <dbl>, `1995` <dbl>, `1994` <dbl>, `1993` <dbl>,
#   `1992` <dbl>, `1991` <dbl>, `1990` <chr>
emissions_longer <- emissions |> 
  filter(!is.na(ISO)) |> 
  mutate(across(.cols = 7:37, .fns = as.numeric)) |>
  pivot_longer(
    cols = "1990":"2020",
    names_to = "year",
    values_to = "emissions"
  ) |> 
  mutate(
    year = as.numeric(year)
  )
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(.cols = 7:37, .fns = as.numeric)`.
Caused by warning:
! NAs introduced by coercion
library(dplyr)
library(countrycode)

emissions_longer$ISO <- as.character(emissions_longer$ISO)

emissions_longer$Country <- countrycode(sourcevar = emissions_longer$ISO, origin = "iso3c", destination = "country.name")
Warning: Some values were not matched unambiguously: EUU, WORLD
emissions_longer$Country[is.na(emissions_longer$Country) & emissions_longer$ISO == "SOME_CODE"] <- "Custom Country Name"


emissions_longer
# A tibble: 6,045 × 8
   ISO   Country `Data source` Sector               Gas    Unit   year emissions
   <chr> <chr>   <chr>         <chr>                <chr>  <chr> <dbl>     <dbl>
 1 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1990    32661.
 2 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1991    32836.
 3 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1992    32736.
 4 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1993    32884.
 5 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1994    33163.
 6 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1995    33961.
 7 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1996    34351 
 8 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1997    35715 
 9 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1998    35261.
10 WORLD <NA>    Climate Watch Total including LUCF All G… MtCO…  1999    35265.
# ℹ 6,035 more rows