使用WDI软件包导入区域内所有国家/地区的世界银行数据

时间:2012-04-17 12:26:34

标签: r

我正在使用RWDI,它允许通过其API导入世界银行数据。问题是我想看一个地区的所有国家,例如撒哈拉以南非洲。但是为此,我需要指定这么多国家(SSH现在是49)。

首先,这是低效的,特别是考虑到data.worldbank.org上的数据资源管理器允许您选择一个区域。

然而,真正的问题是,为了(我猜测)世界银行API而处理国家的数量有问题,因为太多的国家/地区都会出现HTTP错误。导致我不得不将请求分成两部分。

但是,使用效率更高的ALL值时,即使观察次数高得多,也没有错误。

现在我的代码看起来像这样:

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 )

我尝试使用SSH作为国家/地区代码,但这会提供所有SSH国家/地区的汇总,而非所有观察结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:9)

您可以下载所有国家/地区的数据 并使用Region过滤结果。

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)