wb: Download Data from the World Bank API

Description Usage Arguments Value Note Examples

View source: R/deprecated-wb.R

Description

This function downloads the requested information using the World Bank API

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
wb(
  country = "all",
  indicator,
  startdate,
  enddate,
  mrv,
  return_wide = FALSE,
  gapfill,
  freq,
  cache,
  lang = c("en", "es", "fr", "ar", "zh"),
  removeNA = TRUE,
  POSIXct = FALSE,
  include_dec = FALSE,
  include_unit = FALSE,
  include_obsStatus = FALSE,
  include_lastUpdated = FALSE
)

Arguments

country

Character vector of country or region codes. Default value is special code of all. Other permissible values are codes in the following fields from the wb_cachelist country data frame. iso3c, iso2c, regionID, adminID, and incomeID. Additional special values include aggregates, which returns only aggregates, and countries_only, which returns all countries without aggregates.

indicator

Character vector of indicator codes. These codes correspond to the indicatorID column from the indicator data frame of wbcache or wb_cachelist, or the result of wbindicators

startdate

Numeric or character. If numeric it must be in %Y form (i.e. four digit year). For data at the subannual granularity the API supports a format as follows: for monthly data, "2016M01" and for quarterly data, "2016Q1". This also accepts a special value of "YTD", useful for more frequently updated subannual indicators.

enddate

Numeric or character. If numeric it must be in %Y form (i.e. four digit year). For data at the subannual granularity the API supports a format as follows: for monthly data, "2016M01" and for quarterly data, "2016Q1".

mrv

Numeric. The number of Most Recent Values to return. A replacement of startdate and enddate, this number represents the number of observations you which to return starting from the most recent date of collection. Useful in conjuction with freq

return_wide

Logical. If TRUE data is returned in a wide format instead of long, with a column named for each indicatorID. To necessitate this transformation, the indicator column, that provides the human readable description is dropped. This field is available through from the indicator data frame of wbcache or wb_cachelist, or the result of wbindicators. Default is FALSE

gapfill

Logical. Works with mrv. If TRUE fills values, if not available, by back tracking to the next available period (max number of periods back tracked will be limited by mrv number)

freq

Character String. For fetching quarterly ("Q"), monthly("M") or yearly ("Y") values. Currently works along with mrv. Useful for querying high frequency data.

cache

List of data frames returned from wbcache. If omitted, wb_cachelist is used

lang

Language in which to return the results. If lang is unspecified, english is the default.

removeNA

if TRUE, remove any blank or NA observations that are returned. if FALSE, no blank or NA values are removed from the return.

POSIXct

if TRUE, additonal columns date_ct and granularity are added. date_ct converts the default date into a POSIXct. granularity denotes the time resolution that the date represents. Useful for subannual data and mixing subannual with annual data. If FALSE, these fields are not added.

include_dec

if TRUE, the column decimal is not removed from the return. if FALSE, this column is removed

include_unit

if TRUE, the column unit is not removed from the return. if FALSE, this column is removed

include_obsStatus

if TRUE, the column obsStatus is not removed from the return. if FALSE, this column is removed

include_lastUpdated

if TRUE, the column lastUpdated is not removed from the return. if FALSE, this column is removed. If TRUE and POSIXct = TRUE then column will be of class Date

Value

Data frame with all available requested data.

Note

Not all data returns have support for langauges other than english. If the specific return does not support your requested language by default it will return NA. For an enumeration of supported languages by data source please see wbdatacatalog. The options for lang are:

The POSIXct parameter requries the use of lubridate (>= 1.5.0). All dates are rounded down to the floor. For example a value for the year 2016 would have a POSIXct date of 2016-01-01. If this package is not available and the POSIXct parameter is set to TRUE, the parameter is ignored and a warning is produced.

The include_dec, include_unit, and include_obsStatus are defaulted to FALSE because as of writing, all returns have a value of 0, NA, and NA, respectively. These columns might be used in the future by the API, therefore the option to include the column is available.

The include_lastUpdated is defaulted to FALSE as well to limit the

If there is no data available that matches the request parameters, an empty data frame is returned along with a warning. This design is for easy aggregation of multiple calls.

Examples

 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
 # GDP at market prices (current US$) for all available countries and regions
 wb(indicator = "NY.GDP.MKTP.CD", startdate = 2000, enddate = 2016)

 # GDP and Population in long format for the most recent 20 observations
 wb(indicator = c("SP.POP.TOTL","NY.GDP.MKTP.CD"), mrv = 20)

 # GDP and Population in wide format for the most recent 20 observations
 wb(indicator = c("SP.POP.TOTL","NY.GDP.MKTP.CD"), mrv = 20, return_wide = TRUE)

 # query using regionID or incomeID
 # High Income Countries and Sub-Saharan Africa (all income levels)
 wb(country = c("HIC", "SSF"), indicator = "NY.GDP.MKTP.CD", startdate = 1985, enddate = 1985)

 # if you do not know when the latest time an indicator is avaiable mrv can help
 wb(country = c("IN"), indicator = 'EG.ELC.ACCS.ZS', mrv = 1)

 # increase the mrv value to increase the number of maximum number of returns
 wb(country = c("IN"), indicator = 'EG.ELC.ACCS.ZS', mrv = 35)

 # GDP at market prices (current US$) for only available countries
 wb(country = "countries_only", indicator = "NY.GDP.MKTP.CD", startdate = 2000, enddate = 2016)

 # GDP at market prices (current US$) for only available aggregate regions
 wb(country = "aggregates", indicator = "NY.GDP.MKTP.CD", startdate = 2000, enddate = 2016)

 # if you want to "fill-in" the values in between actual observations use gapfill = TRUE
 # this highlights a very important difference.
 # all other parameters are the same as above, except gapfill = TRUE
 # and the results are very different
 wb(country = c("IN"), indicator = 'EG.ELC.ACCS.ZS', mrv = 35, gapfill = TRUE)

 # if you want the most recent values within a certain time frame
 wb(country = c("US"), indicator = 'SI.DST.04TH.20', startdate = 1970, enddate = 2000, mrv = 2)

 # without the freq parameter the deafult temporal granularity search is yearly
 # should return the 12 most recent years of data
 wb(country = c("CHN", "IND"), indicator = "DPANUSSPF", mrv = 12)

 # if another frequency is available for that indicator it can be accessed using the freq parameter
 # should return the 12 most recent months of data
 wb(country = c("CHN", "IND"), indicator = "DPANUSSPF", mrv = 12, freq = "M")

Example output

Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(indicator = "NY.GDP.MKTP.CD", startdate = 2000, enddate = 2016) :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD"), mrv = 20) :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] date    iso2c   country iso3c  
<0 rows> (or 0-length row.names)
Warning message:
In wb(indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD"), mrv = 20,  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("HIC", "SSF"), indicator = "NY.GDP.MKTP.CD", startdate = 1985,  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("IN"), indicator = "EG.ELC.ACCS.ZS", mrv = 1) :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("IN"), indicator = "EG.ELC.ACCS.ZS", mrv = 35) :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = "countries_only", indicator = "NY.GDP.MKTP.CD",  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = "aggregates", indicator = "NY.GDP.MKTP.CD", startdate = 2000,  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("IN"), indicator = "EG.ELC.ACCS.ZS", mrv = 35,  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("US"), indicator = "SI.DST.04TH.20", startdate = 1970,  :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("CHN", "IND"), indicator = "DPANUSSPF", mrv = 12) :
  No data was returned for any requested country and indicator. Returning empty data frame
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Could not resolve host: api.worldbank.org
[1] value       date        indicatorID indicator   iso2c       country    
[7] iso3c      
<0 rows> (or 0-length row.names)
Warning message:
In wb(country = c("CHN", "IND"), indicator = "DPANUSSPF", mrv = 12,  :
  No data was returned for any requested country and indicator. Returning empty data frame

wbstats documentation built on Jan. 13, 2021, 12:19 p.m.