Data Cleanup

Author

Jingning Feng

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.1     ✔ purrr   1.0.1
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.4     ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(janitor)

Attaching package: 'janitor'

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

    chisq.test, fisher.test
library(readxl)
library(countrycode)
country_name_regex_to_iso3c <- function(country_name) {
  country_name |>
    countrycode(origin = "country.name", 
                destination = "iso3c",
                origin_regex = TRUE)
}

iso3c_to_country_name <- function(iso3c) {
  iso3c |>
  countrycode(origin = "iso3c", destination = "country.name")
}
raw_scope1 <- read_excel("RAW_DATA/Emissions of Exports of Goods and Services.xlsm", skip = 6) |> 
  filter(!(Time == "Country"))
New names:
• `` -> `...2`
• `` -> `...4`
• `` -> `...6`
• `` -> `...8`
• `` -> `...10`
• `` -> `...12`
• `` -> `...14`
• `` -> `...16`
• `` -> `...18`
• `` -> `...20`
• `` -> `...22`
• `` -> `...24`
• `` -> `...26`
• `` -> `...28`
• `` -> `...30`
• `` -> `...32`
• `` -> `...34`
• `` -> `...36`
• `` -> `...38`
• `` -> `...40`
• `` -> `...42`
• `` -> `...44`
• `` -> `...46`
• `` -> `...48`
• `` -> `...50`
clean_scope1 <- raw_scope1 |> 
  mutate(iso3c = country_name_regex_to_iso3c(Time)) |> 
  mutate(country_name = iso3c_to_country_name(iso3c))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `iso3c = country_name_regex_to_iso3c(Time)`.
Caused by warning in `countrycode_convert()`:
! Some values were not matched unambiguously: APEC: Asia-Pacific Economic Cooperation, ASEAN: Association of South East Asian Nations, Data extracted on 09 Mar 2023 15:28 UTC (GMT) from OECD.Stat, EA19: Euro area (19 countries), EASIA: Eastern Asia, EU13: EU28 excluding EU15, EU15: European Union (15 countries), EU27_2020: European Union (27 countries), EU28: European Union (28 countries), G20: Group of Twenty, NONOECD: Non-OECD economies and aggregates, OECD: OECD member countries, ROW: Rest of the World, TUR: Türkiye, WLD: World, ZASI: East and Southeastern Asia, ZEUR: Europe, ZNAM: North America, ZOTH: Other regions, ZSCA: South and Central America
#scope2 <- read_excel("RAW_DATA/Grid-supplied electricity, steam and cooling imports.xlsm", skip = 6)
#scope3 <- read_excel("RAW_DATA/Non-energy imports.xlsm", skip = 6)
clean_scope1
# A tibble: 85 × 52
   Time     ...2  `1995`   ...4 `1996`   ...6 `1997`   ...8 `1998`  ...10 `1999`
   <chr>    <lgl> <lgl>   <dbl> <lgl>   <dbl> <lgl>   <dbl> <lgl>   <dbl> <lgl> 
 1 WLD: Wo… NA    NA     2.14e4 NA     2.18e4 NA     2.22e4 NA     2.24e4 NA    
 2 OECD: O… NA    NA     1.30e4 NA     1.33e4 NA     1.35e4 NA     1.37e4 NA    
 3 AUS: Au… NA    NA     2.78e2 NA     2.84e2 NA     2.98e2 NA     3.22e2 NA    
 4 AUT: Au… NA    NA     8.51e1 NA     8.94e1 NA     8.36e1 NA     8.57e1 NA    
 5 BEL: Be… NA    NA     1.30e2 NA     1.32e2 NA     1.27e2 NA     1.31e2 NA    
 6 CAN: Ca… NA    NA     4.09e2 NA     4.17e2 NA     4.36e2 NA     4.39e2 NA    
 7 CHL: Ch… NA    NA     3.99e1 NA     4.62e1 NA     5.31e1 NA     5.66e1 NA    
 8 COL: Co… NA    NA     6.55e1 NA     6.56e1 NA     7.22e1 NA     7.23e1 NA    
 9 CRI: Co… NA    NA     8.05e0 NA     7.77e0 NA     8.17e0 NA     9.12e0 NA    
10 CZE: Cz… NA    NA     1.05e2 NA     1.09e2 NA     1.06e2 NA     9.98e1 NA    
# … with 75 more rows, and 41 more variables: ...12 <dbl>, `2000` <lgl>,
#   ...14 <dbl>, `2001` <lgl>, ...16 <dbl>, `2002` <lgl>, ...18 <dbl>,
#   `2003` <lgl>, ...20 <dbl>, `2004` <lgl>, ...22 <dbl>, `2005` <lgl>,
#   ...24 <dbl>, `2006` <lgl>, ...26 <dbl>, `2007` <lgl>, ...28 <dbl>,
#   `2008` <lgl>, ...30 <dbl>, `2009` <lgl>, ...32 <dbl>, `2010` <lgl>,
#   ...34 <dbl>, `2011` <lgl>, ...36 <dbl>, `2012` <lgl>, ...38 <dbl>,
#   `2013` <lgl>, ...40 <dbl>, `2014` <lgl>, ...42 <dbl>, `2015` <lgl>, …
#scope2
#scope3