Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing World Bank data for all countries in a region using the WDI package

Tags:

r

I am using the R package WDI which allows importing of World Bank data through their API. The problem is that I want to look at all countries in a region, e.g. Sub-Saharan Africa. However for this I need to specify so many countries (SSH is 49 right now).

To start with this is inefficient, especially considering that the data explorer on data.worldbank.org does allow you to select a region.

However the real problem is that the number of countries becomes problematic to handle for (I' m guessing) the World Bank API, because a too-large number of countries gives an HTTP error. Causing me to have to split the request in two parts.

However, when using the more efficient ALL value there is no error, even though the number of observations is a lot higher.

Right now my code looks like this:

library(WDI)

COUNTRIES1 <- c( "AGO","BEN","BWA","BFA","BDI","CMR","CPV","CAF","TCD","COM","ZAR","COG","CIV","GNQ","ERI","ETH","GAB","GMB","GHA","GNB","GIN","KEN","LSO","LBR","MDG" )
COUNTRIES2 <- c( "MWI","MLI","MRT","MUS","MYT","MOZ","NAM","NER","NGA","RWA","STP","SEN","SYC","SLE","SOM","ZAF","SSD","SDN","SWZ","TZA","TGO","UGA","ZMB","ZWE" )
INDICATORS <- c("NY.GDP.PCAP.KN", "SP.DYN.TFRT.IN", "SP.POP.TOTL")

LONG1 <- WDI( country=COUNTRIES1, indicator=INDICATORS, start=1960, end=2009, extra=FALSE)
LONG2 <- WDI( country=COUNTRIES2, indicator=INDICATORS, start=1960, end=2009, extra=FALSE)

LONG <- merge( LONG1, LONG2, by=intersect( names(LONG1),names(LONG2) ), all=TRUE )

I have tried using SSH as a country code, but this gives an aggregate of all SSH countries, rather than all the observations.

Any idea's?

like image 387
Bastiaan Quast Avatar asked Apr 17 '12 12:04

Bastiaan Quast


1 Answers

You can download the data for all countries and use the Region to filter the results.

library(WDI)
indicators <- c("NY.GDP.PCAP.KN", "SP.DYN.TFRT.IN", "SP.POP.TOTL")
d <- WDI("all", indicators, extra=TRUE, start=1960, end=2009)
# Discard unwanted rows
d <- d[ which(d$Region == "Sub-Saharan Africa"), ]
# Discard unwanted columns
d <- d[,1:6]
head(d)
like image 143
Vincent Zoonekynd Avatar answered Oct 19 '22 14:10

Vincent Zoonekynd