14 Working with multiple datasets

14.1 Reading in multiple files

# extracts file names in working directory
worldbank_filenames<-list.files()
# prints file names in working directory
worldbank_filenames
## [1] "wdi_debt2019.csv"  "wdi_fdi2019.csv"   "wdi_trade2019.csv" "wdi_urban2019.csv"
setwd("data/wb")
# iteratively reads in World Bank files from working directory and assigns them to a new list object named "world_bank_list"
world_bank_list<-map(worldbank_filenames, read_csv)
## Rows: 271 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Country Name, Country Code, Series Name, Series Code, 2019 [YR2019]
## 
## ℹ 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.
## Rows: 271 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Country Name, Country Code, Series Name, Series Code, 2019 [YR2019]
## 
## ℹ 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.
## Rows: 271 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Country Name, Country Code, Series Name, Series Code, 2019 [YR2019]
## 
## ℹ 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.
## Rows: 271 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Country Name, Country Code, Series Name, Series Code, 2019 [YR2019]
## 
## ℹ 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.
# prints datasets in "world_bank_list"
world_bank_list
## [[1]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`                          `Series Code` `2019 [YR2019]`
##    <chr>               <chr>          <chr>                                  <chr>         <chr>          
##  1 Afghanistan         AFG            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  2 Albania             ALB            Central government debt, total (% of … GC.DOD.TOTL.… 75.69848824949…
##  3 Algeria             DZA            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  4 American Samoa      ASM            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  5 Andorra             AND            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  6 Angola              AGO            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  7 Antigua and Barbuda ATG            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  8 Argentina           ARG            Central government debt, total (% of … GC.DOD.TOTL.… ..             
##  9 Armenia             ARM            Central government debt, total (% of … GC.DOD.TOTL.… 50.02842068637…
## 10 Aruba               ABW            Central government debt, total (% of … GC.DOD.TOTL.… ..             
## # ℹ 261 more rows
## 
## [[2]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`                          `Series Code` `2019 [YR2019]`
##    <chr>               <chr>          <chr>                                  <chr>         <chr>          
##  1 Afghanistan         AFG            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.124495985791…
##  2 Albania             ALB            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.797920483865…
##  3 Algeria             DZA            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.804144058246…
##  4 American Samoa      ASM            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  5 Andorra             AND            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  6 Angola              AGO            Foreign direct investment, net inflow… BX.KLT.DINV.… -5.78081314444…
##  7 Antigua and Barbuda ATG            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.433324076307…
##  8 Argentina           ARG            Foreign direct investment, net inflow… BX.KLT.DINV.… 1.485006875706…
##  9 Armenia             ARM            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.736361516844…
## 10 Aruba               ABW            Foreign direct investment, net inflow… BX.KLT.DINV.… -2.21528256776…
## # ℹ 261 more rows
## 
## [[3]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`    `Series Code`  `2019 [YR2019]` 
##    <chr>               <chr>          <chr>            <chr>          <chr>           
##  1 Afghanistan         AFG            Trade (% of GDP) NE.TRD.GNFS.ZS ..              
##  2 Albania             ALB            Trade (% of GDP) NE.TRD.GNFS.ZS 76.2791946495763
##  3 Algeria             DZA            Trade (% of GDP) NE.TRD.GNFS.ZS 51.8097384415762
##  4 American Samoa      ASM            Trade (% of GDP) NE.TRD.GNFS.ZS 156.568778979907
##  5 Andorra             AND            Trade (% of GDP) NE.TRD.GNFS.ZS ..              
##  6 Angola              AGO            Trade (% of GDP) NE.TRD.GNFS.ZS 57.8295381183036
##  7 Antigua and Barbuda ATG            Trade (% of GDP) NE.TRD.GNFS.ZS 137.625175755884
##  8 Argentina           ARG            Trade (% of GDP) NE.TRD.GNFS.ZS 32.6306150458499
##  9 Armenia             ARM            Trade (% of GDP) NE.TRD.GNFS.ZS 96.1141541288708
## 10 Aruba               ABW            Trade (% of GDP) NE.TRD.GNFS.ZS 145.343572735289
## # ℹ 261 more rows
## 
## [[4]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`                          `Series Code` `2019 [YR2019]`
##    <chr>               <chr>          <chr>                                  <chr>         <chr>          
##  1 Afghanistan         AFG            Urban population (% of total populati… SP.URB.TOTL.… 25.754         
##  2 Albania             ALB            Urban population (% of total populati… SP.URB.TOTL.… 61.229         
##  3 Algeria             DZA            Urban population (% of total populati… SP.URB.TOTL.… 73.189         
##  4 American Samoa      ASM            Urban population (% of total populati… SP.URB.TOTL.… 87.147         
##  5 Andorra             AND            Urban population (% of total populati… SP.URB.TOTL.… 87.984         
##  6 Angola              AGO            Urban population (% of total populati… SP.URB.TOTL.… 66.177         
##  7 Antigua and Barbuda ATG            Urban population (% of total populati… SP.URB.TOTL.… 24.506         
##  8 Argentina           ARG            Urban population (% of total populati… SP.URB.TOTL.… 91.991         
##  9 Armenia             ARM            Urban population (% of total populati… SP.URB.TOTL.… 63.219         
## 10 Aruba               ABW            Urban population (% of total populati… SP.URB.TOTL.… 43.546         
## # ℹ 261 more rows
# removes CSV extension from "worldbank_filenames"
worldbank_filenames_base<-str_remove(worldbank_filenames, ".csv")

# assigns names to datasets in "world_bank_list"
names(world_bank_list)<-worldbank_filenames_base
# extracts fdi dataset from "world_bank_list" by assigned name
world_bank_list[["wdi_fdi2019"]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`                          `Series Code` `2019 [YR2019]`
##    <chr>               <chr>          <chr>                                  <chr>         <chr>          
##  1 Afghanistan         AFG            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.124495985791…
##  2 Albania             ALB            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.797920483865…
##  3 Algeria             DZA            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.804144058246…
##  4 American Samoa      ASM            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  5 Andorra             AND            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  6 Angola              AGO            Foreign direct investment, net inflow… BX.KLT.DINV.… -5.78081314444…
##  7 Antigua and Barbuda ATG            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.433324076307…
##  8 Argentina           ARG            Foreign direct investment, net inflow… BX.KLT.DINV.… 1.485006875706…
##  9 Armenia             ARM            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.736361516844…
## 10 Aruba               ABW            Foreign direct investment, net inflow… BX.KLT.DINV.… -2.21528256776…
## # ℹ 261 more rows
# extracts fdi dataset from "world_bank_list" by index
world_bank_list[[2]]
## # A tibble: 271 × 5
##    `Country Name`      `Country Code` `Series Name`                          `Series Code` `2019 [YR2019]`
##    <chr>               <chr>          <chr>                                  <chr>         <chr>          
##  1 Afghanistan         AFG            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.124495985791…
##  2 Albania             ALB            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.797920483865…
##  3 Algeria             DZA            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.804144058246…
##  4 American Samoa      ASM            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  5 Andorra             AND            Foreign direct investment, net inflow… BX.KLT.DINV.… ..             
##  6 Angola              AGO            Foreign direct investment, net inflow… BX.KLT.DINV.… -5.78081314444…
##  7 Antigua and Barbuda ATG            Foreign direct investment, net inflow… BX.KLT.DINV.… 7.433324076307…
##  8 Argentina           ARG            Foreign direct investment, net inflow… BX.KLT.DINV.… 1.485006875706…
##  9 Armenia             ARM            Foreign direct investment, net inflow… BX.KLT.DINV.… 0.736361516844…
## 10 Aruba               ABW            Foreign direct investment, net inflow… BX.KLT.DINV.… -2.21528256776…
## # ℹ 261 more rows

14.2 Merging Data

# extracts World Bank FDI dataset and assigns it to a new object named WB_fdi
WB_fdi<-world_bank_list[[2]]
# extracts World Bank debt dataset and assigns it to a new object named WB_debt
WB_debt<-world_bank_list[[1]]

14.3 Appending Data

14.4 Reshaping Data

14.5 Automating data processing tasks

# write function to World Bank dataset
worldbank_cleaning_function<-function(input_dataset){
  modified_dataset<-input_dataset %>% 
                      select(-"Series Code") %>% 
                      rename("Country"="Country Name",
                             "CountryCode"="Country Code",
                             "Series"="Series Name",
                             "2019"="2019 [YR2019]") %>% 
                      drop_na(CountryCode)
  return(modified_dataset)
}
# Iteratively apply "worldbank_cleaning_function" to all of the datasets in "world_bank_list", and deposit the cleaned datasets into a new list named "world_bank_list_cleaned"
world_bank_list_cleaned<-map(world_bank_list, worldbank_cleaning_function)