-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadWBIndicators.R
More file actions
55 lines (34 loc) · 1.69 KB
/
ReadWBIndicators.R
File metadata and controls
55 lines (34 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Replacement for WDIData github library
options(digits = 17)
getWDIData <- function(indicator,date){
pagingQuery <- paste('http://api.worldbank.org/v2/countries/all/indicators/',as.character(indicator),
'?date=',as.character(date),'&format=json',sep="");
initialQuery <- fromJSON(pagingQuery)
initialQuery <- initialQuery[[1]]
message(paste0('Fetching indicator ',indicator,': Total Items ',initialQuery$total,' Total Pages: ', initialQuery$pages))
completeDataSet <- matrix(ncol = 4,nrow = 0);
# Iterate through all pages of indicator
for(page in 1:initialQuery$pages){
normalQuery <- paste0('http://api.worldbank.org/v2/countries/all/indicators/',indicator,
'?date=',date,'&page=',page,'&format=json');
dataSet <- fromJSON(normalQuery)[[2]]
#For each page get all the 5 values needed
for(iterator in 1:length(dataSet)){
v <- c(dataSet[[iterator]]$country['id'] %>% unname,
dataSet[[iterator]]$countryiso3code,
dataSet[[iterator]]$date,
if(is.null(dataSet[[iterator]]$value)) NA else dataSet[[iterator]]$value)
completeDataSet <- rbind(completeDataSet,v)
}
}
completeDataSet <- completeDataSet %>% as.data.frame
setnames(completeDataSet,c(paste0('V',1:4)),c('iso2c','iso3c','year','value'))
factorToValue <- function(val){
as.numeric(as.character(val))
}
completeDataSet$year <- sapply(completeDataSet[,3],factorToValue)
completeDataSet$value <- sapply(completeDataSet[,4],factorToValue)
return(completeDataSet)
}
# Call to API
#myData <- getWDIData(indicator = 'NY.GDP.MKTP.CD',date= '2017:2017')