worldbank

Lifecycle: experimental R-CMD-check CRAN status

Overview

worlbank provides a simple interface to the World Bank Indicators API v2 and to the World Bank Poverty and Inequality Platform (PIP) API. The main difference to other packages is that it’s a modern implementation using the httr2 package and supports all available endpoints and parameters.

The worldbank package provides a set of functions to interact with various endpoints of the World Bank Indicators API. Each function is designed to retrieve specific types of data, making it easier to access and analyze World Bank datasets. Below is an overview of the available endpoints and their corresponding functions in the package:

Installation

You can install the released version of worldbank from CRAN with:

install.packages("worldbank")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("m-muecke/worldbank")

Usage

worldbank functions are prefixed with wb_ and follow the naming convention of the World Bank API v2.

library(worldbank)

# filter by specific country
wb_country(c("US", "DE"))
#> # A tibble: 2 × 18
#>   country_id country_code country_name  region_id region_code region_value      
#>   <chr>      <chr>        <chr>         <chr>     <chr>       <chr>             
#> 1 DEU        DE           Germany       ECS       Z7          Europe & Central …
#> 2 USA        US           United States NAC       XU          North America     
#> # ℹ 12 more variables: admin_region_id <chr>, admin_region_code <chr>,
#> #   admin_region_value <chr>, income_level_id <chr>, income_level_code <chr>,
#> #   income_level_value <chr>, lending_type_id <chr>, lending_type_code <chr>,
#> #   lending_type_value <chr>, capital_city <chr>, longitude <dbl>,
#> #   latitude <dbl>

# or fetch all (default)
wb_country()
#> # A tibble: 296 × 18
#>   country_id country_code country_name        region_id region_code region_value
#>   <chr>      <chr>        <chr>               <chr>     <chr>       <chr>       
#> 1 ABW        AW           Aruba               LCN       ZJ          Latin Ameri…
#> 2 AFE        ZH           Africa Eastern and… NA        NA          Aggregates  
#> 3 AFG        AF           Afghanistan         SAS       8S          South Asia  
#> 4 AFR        A9           Africa              NA        NA          Aggregates  
#> 5 AFW        ZI           Africa Western and… NA        NA          Aggregates  
#> # ℹ 291 more rows
#> # ℹ 12 more variables: admin_region_id <chr>, admin_region_code <chr>,
#> #   admin_region_value <chr>, income_level_id <chr>, income_level_code <chr>,
#> #   income_level_value <chr>, lending_type_id <chr>, lending_type_code <chr>,
#> #   lending_type_value <chr>, capital_city <chr>, longitude <dbl>,
#> #   latitude <dbl>

# search for specific indicator
ind <- wb_indicator()
ind <- subset(
  ind,
  grepl("GDP", id, fixed = TRUE) & source_value == "World Development Indicators"
)
ind
#> # A tibble: 35 × 9
#>   id          name  unit  source_id source_value source_note source_organization
#>   <chr>       <chr> <chr> <chr>     <chr>        <chr>       <chr>              
#> 1 EG.GDP.PUS… GDP … <NA>  2         World Devel… GDP per un… IEA Statistics © O…
#> 2 EG.GDP.PUS… GDP … <NA>  2         World Devel… GDP per un… IEA Statistics © O…
#> 3 ER.GDP.FWT… Wate… <NA>  2         World Devel… Water prod… Food and Agricultu…
#> 4 NY.GDP.COA… Coal… <NA>  2         World Devel… Coal rents… World Bank staff e…
#> 5 NY.GDP.DEF… Infl… <NA>  2         World Devel… Inflation … World Bank nationa…
#> # ℹ 30 more rows
#> # ℹ 2 more variables: topic_id <chr>, topic_value <chr>

# fetch indicator data for specific or all countries (default)
gdp <- wb_country_indicator("NY.GDP.MKTP.CD", c("US", "DE", "FR", "CH", "JP"))
gdp
#> # A tibble: 320 × 10
#>    date indicator_id indicator_name country_id country_name country_code   value
#>   <int> <chr>        <chr>          <chr>      <chr>        <chr>          <dbl>
#> 1  2023 NY.GDP.MKTP… GDP (current … CH         Switzerland  CHE          8.85e11
#> 2  2022 NY.GDP.MKTP… GDP (current … CH         Switzerland  CHE          8.18e11
#> 3  2021 NY.GDP.MKTP… GDP (current … CH         Switzerland  CHE          8.13e11
#> 4  2020 NY.GDP.MKTP… GDP (current … CH         Switzerland  CHE          7.42e11
#> 5  2019 NY.GDP.MKTP… GDP (current … CH         Switzerland  CHE          7.21e11
#> # ℹ 315 more rows
#> # ℹ 3 more variables: unit <chr>, obs_status <chr>, decimal <int>