The World Bank collects statistical information from countries around the world. A particularly useful data set is the World Development Indicators (WDI) which are country level statistical information from around the world.
R is unique in that using library(WDI) you can download indicator data directly from the World Bank, read it into a data set, and put it to use. Using library(plotly) you can even make cool looking motion charts, somewhat reminiscent of those popularized by Hans Rosling.
While the code below is seemingly arcane, it is important to recognize that it is simple in structure. It is very possible to re-purpose the code below using some of the many 1,000’s of WDI indicators that are of interest to you.
2 Call Relevant Libraries
Show the code
library(WDI) # for accessing World Bank datalibrary(dplyr) # data wranglinglibrary(ggplot2) # beautiful graphslibrary(plotly) # for updated version of cool Hans Rosling style visualizationslibrary(DT) # data tableoptions(scipen =999) # penalize scientific notation
3 Get Some Data From the World Development Indicators (WDI)
Show the code
# get names of specific indicators from WDI Data Catalogmydata <-WDI(country="all", indicator=c("SI.POV.GINI", # Gini"NY.GDP.PCAP.CD", # GDP"SE.ADT.LITR.ZS", # adult literacy"SP.DYN.LE00.IN", # life expectancy"VC.BTL.DETH", # battle related deaths"SP.POP.TOTL", # population"SN.ITK.DEFC.ZS"), # undernourishmentstart =1980, end =2023, extra =TRUE) save(mydata, file="WorldBankData.RData")
4 Rename Some Variables
Show the code
# think about renaming some variables with more intuitive names# e.g....# rename some variables with dplyr (just copy and paste your indicators)mydata <- dplyr::rename(mydata, GDP = NY.GDP.PCAP.CD,adult_literacy = SE.ADT.LITR.ZS,life_expectancy = SP.DYN.LE00.IN, battle_death = VC.BTL.DETH,population = SP.POP.TOTL,Gini = SI.POV.GINI,undernourishment = SN.ITK.DEFC.ZS)mydata$country_name <- mydata$countrymydata$country <-as.factor(mydata$country)save(mydata, file="WorldBankData.RData")