Cleaning

# Install packages
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.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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(rnaturalearth)
Warning: package 'rnaturalearth' was built under R version 4.5.2
library(rnaturalearthdata)
Warning: package 'rnaturalearthdata' was built under R version 4.5.2

Attaching package: 'rnaturalearthdata'

The following object is masked from 'package:rnaturalearth':

    countries110
library(countrycode)
Warning: package 'countrycode' was built under R version 4.5.2

Infant mortality rate dataset

Importing data

# Import Infant Mortality dataset
df <- read_csv("infant-mortality.csv")
Rows: 13577 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Entity, Code
dbl (2): Year, Infant mortality rate of babies aged under one year, per 100 ...

ℹ 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.

Inspecting data

# Examine country codes in dataset
unique(df$Code)
  [1] "AFG"      NA         "ALB"      "DZA"      "AND"      "AGO"     
  [7] "AIA"      "ATG"      "ARG"      "ARM"      "AUS"      "AUT"     
 [13] "AZE"      "BHS"      "BHR"      "BGD"      "BRB"      "BLR"     
 [19] "BEL"      "BLZ"      "BEN"      "BTN"      "BOL"      "BIH"     
 [25] "BWA"      "BRA"      "VGB"      "BRN"      "BGR"      "BFA"     
 [31] "BDI"      "KHM"      "CMR"      "CAN"      "CPV"      "CAF"     
 [37] "TCD"      "CHL"      "CHN"      "COL"      "COM"      "COG"     
 [43] "COK"      "CRI"      "CIV"      "HRV"      "CUB"      "CYP"     
 [49] "CZE"      "COD"      "DNK"      "DJI"      "DMA"      "DOM"     
 [55] "TLS"      "ECU"      "EGY"      "SLV"      "GNQ"      "ERI"     
 [61] "EST"      "SWZ"      "ETH"      "FJI"      "FIN"      "FRA"     
 [67] "GAB"      "GMB"      "GEO"      "DEU"      "GHA"      "GRC"     
 [73] "GRD"      "GTM"      "GIN"      "GNB"      "GUY"      "HTI"     
 [79] "HND"      "HUN"      "ISL"      "IND"      "IDN"      "IRN"     
 [85] "IRQ"      "IRL"      "ISR"      "ITA"      "JAM"      "JPN"     
 [91] "JOR"      "KAZ"      "KEN"      "KIR"      "OWID_KOS" "KWT"     
 [97] "KGZ"      "LAO"      "LVA"      "LBN"      "LSO"      "LBR"     
[103] "LBY"      "LTU"      "LUX"      "MDG"      "MWI"      "MYS"     
[109] "MDV"      "MLI"      "MLT"      "MHL"      "MRT"      "MUS"     
[115] "MEX"      "FSM"      "MDA"      "MCO"      "MNG"      "MNE"     
[121] "MSR"      "MAR"      "MOZ"      "MMR"      "NAM"      "NRU"     
[127] "NPL"      "NLD"      "NZL"      "NIC"      "NER"      "NGA"     
[133] "NIU"      "PRK"      "MKD"      "NOR"      "OMN"      "PAK"     
[139] "PLW"      "PSE"      "PAN"      "PNG"      "PRY"      "PER"     
[145] "PHL"      "POL"      "PRT"      "QAT"      "ROU"      "RUS"     
[151] "RWA"      "KNA"      "LCA"      "VCT"      "WSM"      "SMR"     
[157] "STP"      "SAU"      "SEN"      "SRB"      "SYC"      "SLE"     
[163] "SGP"      "SVK"      "SVN"      "SLB"      "SOM"      "ZAF"     
[169] "KOR"      "SSD"      "ESP"      "LKA"      "SDN"      "SUR"     
[175] "SWE"      "CHE"      "SYR"      "TJK"      "TZA"      "THA"     
[181] "TGO"      "TON"      "TTO"      "TUN"      "TUR"      "TKM"     
[187] "TCA"      "TUV"      "UGA"      "UKR"      "ARE"      "GBR"     
[193] "USA"      "URY"      "UZB"      "VUT"      "VEN"      "VNM"     
[199] "OWID_WRL" "YEM"      "ZMB"      "ZWE"     
unique(df$Entity)
  [1] "Afghanistan"                      "Africa"                          
  [3] "Albania"                          "Algeria"                         
  [5] "Andorra"                          "Angola"                          
  [7] "Anguilla"                         "Antigua and Barbuda"             
  [9] "Argentina"                        "Armenia"                         
 [11] "Asia"                             "Australia"                       
 [13] "Austria"                          "Azerbaijan"                      
 [15] "Bahamas"                          "Bahrain"                         
 [17] "Bangladesh"                       "Barbados"                        
 [19] "Belarus"                          "Belgium"                         
 [21] "Belize"                           "Benin"                           
 [23] "Bhutan"                           "Bolivia"                         
 [25] "Bosnia and Herzegovina"           "Botswana"                        
 [27] "Brazil"                           "British Virgin Islands"          
 [29] "Brunei"                           "Bulgaria"                        
 [31] "Burkina Faso"                     "Burundi"                         
 [33] "Cambodia"                         "Cameroon"                        
 [35] "Canada"                           "Cape Verde"                      
 [37] "Central African Republic"         "Chad"                            
 [39] "Chile"                            "China"                           
 [41] "Colombia"                         "Comoros"                         
 [43] "Congo"                            "Cook Islands"                    
 [45] "Costa Rica"                       "Cote d'Ivoire"                   
 [47] "Croatia"                          "Cuba"                            
 [49] "Cyprus"                           "Czechia"                         
 [51] "Democratic Republic of Congo"     "Denmark"                         
 [53] "Djibouti"                         "Dominica"                        
 [55] "Dominican Republic"               "East Timor"                      
 [57] "Ecuador"                          "Egypt"                           
 [59] "El Salvador"                      "Equatorial Guinea"               
 [61] "Eritrea"                          "Estonia"                         
 [63] "Eswatini"                         "Ethiopia"                        
 [65] "Europe"                           "European Union (27)"             
 [67] "Fiji"                             "Finland"                         
 [69] "France"                           "Gabon"                           
 [71] "Gambia"                           "Georgia"                         
 [73] "Germany"                          "Ghana"                           
 [75] "Greece"                           "Grenada"                         
 [77] "Guatemala"                        "Guinea"                          
 [79] "Guinea-Bissau"                    "Guyana"                          
 [81] "Haiti"                            "High-income countries"           
 [83] "Honduras"                         "Hungary"                         
 [85] "Iceland"                          "India"                           
 [87] "Indonesia"                        "Iran"                            
 [89] "Iraq"                             "Ireland"                         
 [91] "Israel"                           "Italy"                           
 [93] "Jamaica"                          "Japan"                           
 [95] "Jordan"                           "Kazakhstan"                      
 [97] "Kenya"                            "Kiribati"                        
 [99] "Kosovo"                           "Kuwait"                          
[101] "Kyrgyzstan"                       "Laos"                            
[103] "Latvia"                           "Lebanon"                         
[105] "Lesotho"                          "Liberia"                         
[107] "Libya"                            "Lithuania"                       
[109] "Low-income countries"             "Lower-middle-income countries"   
[111] "Luxembourg"                       "Madagascar"                      
[113] "Malawi"                           "Malaysia"                        
[115] "Maldives"                         "Mali"                            
[117] "Malta"                            "Marshall Islands"                
[119] "Mauritania"                       "Mauritius"                       
[121] "Mexico"                           "Micronesia (country)"            
[123] "Moldova"                          "Monaco"                          
[125] "Mongolia"                         "Montenegro"                      
[127] "Montserrat"                       "Morocco"                         
[129] "Mozambique"                       "Myanmar"                         
[131] "Namibia"                          "Nauru"                           
[133] "Nepal"                            "Netherlands"                     
[135] "New Zealand"                      "Nicaragua"                       
[137] "Niger"                            "Nigeria"                         
[139] "Niue"                             "North America"                   
[141] "North Korea"                      "North Macedonia"                 
[143] "Norway"                           "Oceania"                         
[145] "Oman"                             "Pakistan"                        
[147] "Palau"                            "Palestine"                       
[149] "Panama"                           "Papua New Guinea"                
[151] "Paraguay"                         "Peru"                            
[153] "Philippines"                      "Poland"                          
[155] "Portugal"                         "Qatar"                           
[157] "Romania"                          "Russia"                          
[159] "Rwanda"                           "Saint Kitts and Nevis"           
[161] "Saint Lucia"                      "Saint Vincent and the Grenadines"
[163] "Samoa"                            "San Marino"                      
[165] "Sao Tome and Principe"            "Saudi Arabia"                    
[167] "Senegal"                          "Serbia"                          
[169] "Seychelles"                       "Sierra Leone"                    
[171] "Singapore"                        "Slovakia"                        
[173] "Slovenia"                         "Solomon Islands"                 
[175] "Somalia"                          "South Africa"                    
[177] "South America"                    "South Korea"                     
[179] "South Sudan"                      "Spain"                           
[181] "Sri Lanka"                        "Sudan"                           
[183] "Suriname"                         "Sweden"                          
[185] "Switzerland"                      "Syria"                           
[187] "Tajikistan"                       "Tanzania"                        
[189] "Thailand"                         "Togo"                            
[191] "Tonga"                            "Trinidad and Tobago"             
[193] "Tunisia"                          "Turkey"                          
[195] "Turkmenistan"                     "Turks and Caicos Islands"        
[197] "Tuvalu"                           "Uganda"                          
[199] "Ukraine"                          "United Arab Emirates"            
[201] "United Kingdom"                   "United States"                   
[203] "Upper-middle-income countries"    "Uruguay"                         
[205] "Uzbekistan"                       "Vanuatu"                         
[207] "Venezuela"                        "Vietnam"                         
[209] "World"                            "Yemen"                           
[211] "Zambia"                           "Zimbabwe"                        
str(df)
spc_tbl_ [13,577 × 4] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Entity                                                                  : chr [1:13577] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
 $ Code                                                                    : chr [1:13577] "AFG" "AFG" "AFG" "AFG" ...
 $ Year                                                                    : num [1:13577] 1957 1958 1959 1960 1961 ...
 $ Infant mortality rate of babies aged under one year, per 100 live births: num [1:13577] 26.1 25.8 25.5 25.1 24.8 ...
 - attr(*, "spec")=
  .. cols(
  ..   Entity = col_character(),
  ..   Code = col_character(),
  ..   Year = col_double(),
  ..   `Infant mortality rate of babies aged under one year, per 100 live births` = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
# Rename columns
df2 <- df |>
  rename(`entity` = Entity,
         `code` = Code,
         `year` = Year,
         `rate` = `Infant mortality rate of babies aged under one year, per 100 live births`)
# Adding continent information to dataset
df3 <- df2 |> 
  mutate(continent = countrycode(sourcevar = code, 
                              origin = "iso3c",
                              destination = "continent"))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `continent = countrycode(sourcevar = code, origin = "iso3c",
  destination = "continent")`.
Caused by warning:
! Some values were not matched unambiguously: OWID_KOS, OWID_WRL
unique(df3$continent)
[1] "Asia"     NA         "Europe"   "Africa"   "Americas" "Oceania" 
glimpse(df3)
Rows: 13,577
Columns: 5
$ entity    <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
$ code      <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG…
$ year      <dbl> 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, …
$ rate      <dbl> 26.09423, 25.77640, 25.46791, 25.12314, 24.83928, 24.53919, …
$ continent <chr> "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asi…
# 
world <- ne_countries(scale = "medium", returnclass = "sf") |>
  select(iso_a3, geometry)

glimpse(world)
Rows: 242
Columns: 2
$ iso_a3   <chr> "ZWE", "ZMB", "YEM", "VNM", "VEN", "VAT", "VUT", "UZB", "URY"…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((31.28789 -2..., MULTIPOLYGON (((…
df_clean <- df3 |>
  # Merge world map data with infant mortality data. I wanted to 
  left_join(world, by = c("code" = "iso_a3"))

glimpse(df_clean)
Rows: 13,577
Columns: 6
$ entity    <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
$ code      <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG…
$ year      <dbl> 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, …
$ rate      <dbl> 26.09423, 25.77640, 25.46791, 25.12314, 24.83928, 24.53919, …
$ continent <chr> "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asi…
$ geometry  <MULTIPOLYGON [°]> MULTIPOLYGON (((66.52227 37..., MULTIPOLYGON ((…
# Save infant mortality data
save(df_clean, file = "df_clean.RData")

GDP dataset

df_gdp <- read_csv("gdp-per-capita-worldbank.csv")
Rows: 7311 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): Entity, Code, World regions according to OWID
dbl (2): Year, GDP per capita, PPP (constant 2021 international $)

ℹ 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.
# Inspect data
glimpse(df_gdp)
Rows: 7,311
Columns: 5
$ Entity                                                <chr> "Afghanistan", "…
$ Code                                                  <chr> "AFG", "AFG", "A…
$ Year                                                  <dbl> 2000, 2001, 2002…
$ `GDP per capita, PPP (constant 2021 international $)` <dbl> 1617.826, 1454.1…
$ `World regions according to OWID`                     <chr> NA, NA, NA, NA, …
head(df_gdp, n=30)
# A tibble: 30 × 5
   Entity      Code   Year GDP per capita, PPP (constan…¹ World regions accord…²
   <chr>       <chr> <dbl>                          <dbl> <chr>                 
 1 Afghanistan AFG    2000                          1618. <NA>                  
 2 Afghanistan AFG    2001                          1454. <NA>                  
 3 Afghanistan AFG    2002                          1774. <NA>                  
 4 Afghanistan AFG    2003                          1816. <NA>                  
 5 Afghanistan AFG    2004                          1777. <NA>                  
 6 Afghanistan AFG    2005                          1908. <NA>                  
 7 Afghanistan AFG    2006                          1930. <NA>                  
 8 Afghanistan AFG    2007                          2155. <NA>                  
 9 Afghanistan AFG    2008                          2192. <NA>                  
10 Afghanistan AFG    2009                          2565. <NA>                  
# ℹ 20 more rows
# ℹ abbreviated names: ¹​`GDP per capita, PPP (constant 2021 international $)`,
#   ²​`World regions according to OWID`
# Rename columns
gdp_clean <- df_gdp |>
  rename(`entity` = Entity,
         `code` = Code,
         `year` = Year,
         `gdp_capita` = `GDP per capita, PPP (constant 2021 international $)`) |>
  select(entity, code, gdp_capita, year)

glimpse(gdp_clean)
Rows: 7,311
Columns: 4
$ entity     <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",…
$ code       <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AF…
$ gdp_capita <dbl> 1617.826, 1454.111, 1774.309, 1815.928, 1776.918, 1908.115,…
$ year       <dbl> 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,…
# Save GDP data
save(gdp_clean, file = "gdp_clean.RData")