| Title: | Interface to the World Inequality Database (WID) |
|---|---|
| Description: | Interface to the World Inequality Database (WID) API <https://wid.world>. Downloads distributional national accounts data with filters for country, year, percentile, age group, and population type. Includes code validation and reference tables. Independent implementation unaffiliated with the World Inequality Lab (WIL) or the Paris School of Economics. |
| Authors: | Cheryl Isabella Lim [aut, cre] |
| Maintainer: | Cheryl Isabella Lim <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.1 |
| Built: | 2026-05-21 09:51:50 UTC |
| Source: | https://github.com/cherylisabella/widr |
Interface to the World Inequality Database (WID) API https://wid.world. Downloads distributional national accounts data with filters for country, year, percentile, age group, and population type. Includes code validation and reference tables. Independent implementation unaffiliated with the World Inequality Lab (WIL) or the Paris School of Economics.
Maintainer: Cheryl Isabella Lim [email protected]
Useful links:
Returns a wid_df, a classed data.frame that works natively
with dplyr, ggplot2, and all base-R operations.
download_wid( indicators = "all", areas = "all", years = "all", perc = "all", ages = "992", pop = "j", metadata = FALSE, include_extrapolations = TRUE, verbose = FALSE, cache = TRUE ) download( indicators = "all", areas = "all", years = "all", perc = "all", ages = "992", pop = "j", metadata = FALSE, include_extrapolations = TRUE, verbose = FALSE, cache = TRUE )download_wid( indicators = "all", areas = "all", years = "all", perc = "all", ages = "992", pop = "j", metadata = FALSE, include_extrapolations = TRUE, verbose = FALSE, cache = TRUE ) download( indicators = "all", areas = "all", years = "all", perc = "all", ages = "992", pop = "j", metadata = FALSE, include_extrapolations = TRUE, verbose = FALSE, cache = TRUE )
indicators |
WID variable codes or |
areas |
ISO-2 area codes or |
years |
Integer vector or |
perc |
Percentile codes or |
ages |
Age codes or |
pop |
Population unit codes or |
metadata |
Logical. If |
include_extrapolations |
Logical. Default |
verbose |
Logical. Default |
cache |
Logical. Default |
A wid_df (a classed data.frame) with columns
country, variable, percentile, year,
value, age, pop. If metadata = TRUE,
a "wid_meta" attribute is attached.
if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid( indicators = "sptinc992j", areas = "US", perc = "p99p100", years = 2010:2020 ) }if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid( indicators = "sptinc992j", areas = "US", perc = "p99p100", years = 2010:2020 ) }
This method prints a summary of a wid_df object,
including the number of rows, countries, and variables.
## S3 method for class 'wid_df' print(x, ...)## S3 method for class 'wid_df' print(x, ...)
x |
A |
... |
Additional arguments passed to |
Invisibly returns the input x.
d <- data.frame(country = "US", variable = "sptinc992j", percentile = "p99p100", year = "2020", value = 0.19, age = "992", pop = "j", stringsAsFactors = FALSE) class(d) <- c("wid_df", "data.frame") print(d)d <- data.frame(country = "US", variable = "sptinc992j", percentile = "p99p100", year = "2020", value = 0.19, age = "992", pop = "j", stringsAsFactors = FALSE) class(d) <- c("wid_df", "data.frame") print(d)
Displays the contents of a wid_query object.
## S3 method for class 'wid_query' print(x, ...)## S3 method for class 'wid_query' print(x, ...)
x |
A |
... |
Additional arguments (currently ignored). |
Invisibly returns the input x.
q <- wid_query(indicators = "sptinc992j", areas = "US") print(q)q <- wid_query(indicators = "sptinc992j", areas = "US") print(q)
Zero-padded three-digit strings.
wid_ages wid_ageswid_ages wid_ages
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Low-level access to get, set, list, or clear cached API responses.
wid_cache(action, key = NULL, value = NULL)wid_cache(action, key = NULL, value = NULL)
action |
One of |
key |
Cache key string (required for |
value |
Value to store ( |
For "get": the cached object or NULL. For
"list": character vector of cache keys. For "set" and
"clear": invisibly, the stored value or file count removed.
wid_cache("list")wid_cache("list")
Clear all cached WID API responses
wid_cache_clear()wid_cache_clear()
Invisibly, the number of files removed.
wid_cache_clear()wid_cache_clear()
List cached WID API responses
wid_cache_list()wid_cache_list()
Character vector of cache file names.
wid_cache_list()wid_cache_list()
Letters 2-6 of a WID variable code. Scraped from \S3-6, 8-9 tables; \S7 hardcoded from prose.
wid_concepts wid_conceptswid_concepts wid_concepts
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Convert a wid_df to another currency
wid_convert(data, target = "usd", base_year = NULL)wid_convert(data, target = "usd", base_year = NULL)
data |
A |
target |
One of |
base_year |
Pin to a specific year's exchange rate; |
The same wid_df with values converted. Dimensionless series
(shares, ratios) are returned unchanged with a message.
if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid("aptinc992j", areas = "US", perc = "p99p100", years = 2010:2020) wid_convert(d, target = "ppp") }if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid("aptinc992j", areas = "US", perc = "p99p100", years = 2010:2020) wid_convert(d, target = "ppp") }
Country and region codes (\S2.2)
WID country and region codes
wid_countries wid_countrieswid_countries wid_countries
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Parse a WID variable code into its components
wid_decode(x, strict = TRUE)wid_decode(x, strict = TRUE)
x |
Character string, e.g. |
strict |
|
Named list with elements series_type (character),
concept (character), age (character or NULL),
and pop (character or NULL).
wid_decode("sptinc992j") wid_decode("mnninc") # no age or popwid_decode("sptinc992j") wid_decode("mnninc") # no age or pop
Build a WID variable code from its components
wid_encode(series_type, concept = NULL, age = NULL, pop = NULL)wid_encode(series_type, concept = NULL, age = NULL, pop = NULL)
series_type |
1-letter series type, or a list from |
concept |
5-6 letter concept code. |
age |
3-digit age code or |
pop |
1-letter population code or |
A character string of the form
<type><concept>[age][pop].
wid_encode("s", "ptinc", "992", "j") # "sptinc992j" wid_encode("m", "nninc") # "mnninc" wid_encode(wid_decode("sptinc992j")) # round-tripwid_encode("s", "ptinc", "992", "j") # "sptinc992j" wid_encode("m", "nninc") # "mnninc" wid_encode(wid_decode("sptinc992j")) # round-trip
Fetch data from WID.world using a wid_query object.
wid_fetch(query, ...)wid_fetch(query, ...)
query |
A |
... |
Parameter overrides. |
A wid_df (a classed data.frame); see download_wid.
if (nzchar(Sys.getenv("WID_API_KEY"))) { q <- wid_query(indicators = "sptinc992j", areas = "US", years = 2010:2020) wid_fetch(q) }if (nzchar(Sys.getenv("WID_API_KEY"))) { q <- wid_query(indicators = "sptinc992j", areas = "US", years = 2010:2020) wid_fetch(q) }
Modify fields of a wid_query object, returning an updated query.
wid_filter(query, ...)wid_filter(query, ...)
query |
A |
... |
Fields to update. |
The updated wid_query object.
q <- wid_query(indicators = "sptinc992j", areas = "US") q <- wid_filter(q, years = 2010:2020)q <- wid_query(indicators = "sptinc992j", areas = "US") q <- wid_filter(q, years = 2010:2020)
Requires an "s" (share) series with pXpY percentile codes
covering the full distribution.
wid_gini(data, variable = NULL, country = NULL)wid_gini(data, variable = NULL, country = NULL)
data |
A |
variable |
Variable code filter. |
country |
Country code filter. |
A data.frame with columns country, year,
and gini (numeric, 0-1).
d <- data.frame( country = rep("US", 3L), year = rep("2020", 3L), variable = rep("sptinc992j", 3L), percentile = c("p0p50", "p50p90", "p90p100"), value = c(0.20, 0.40, 0.40), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_gini(d)d <- data.frame( country = rep("US", 3L), year = rep("2020", 3L), variable = rep("sptinc992j", 3L), percentile = c("p0p50", "p50p90", "p90p100"), value = c(0.20, 0.40, 0.40), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_gini(d)
Test WID code components without throwing
wid_is_valid(...)wid_is_valid(...)
... |
Arguments passed to |
Scalar logical: TRUE if all supplied components are valid,
FALSE if any would cause an error. Warning-level issues return
TRUE.
wid_is_valid(series_type = "s", concept = "ptinc") # TRUE wid_is_valid(series_type = "Z") # FALSEwid_is_valid(series_type = "s", concept = "ptinc") # TRUE wid_is_valid(series_type = "Z") # FALSE
Fetch metadata for variables in a wid_df
wid_metadata(data)wid_metadata(data)
data |
A |
A data.frame with columns variable, country,
source, method, quality, and imputation.
if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid("sptinc992j", areas = "US", metadata = TRUE) wid_metadata(d) }if (nzchar(Sys.getenv("WID_API_KEY"))) { d <- download_wid("sptinc992j", areas = "US", metadata = TRUE) wid_metadata(d) }
Percentile threshold ratio (e.g. P90/P10)
wid_percentile_ratio( data, numerator = "p90", denominator = "p10", variable = NULL, country = NULL )wid_percentile_ratio( data, numerator = "p90", denominator = "p10", variable = NULL, country = NULL )
data |
A |
numerator |
Upper percentile code, e.g. |
denominator |
Lower percentile code, e.g. |
variable |
Variable code filter. |
country |
Country code filter. |
A data.frame with columns country, year,
and ratio (numeric: numerator threshold divided by denominator threshold).
d <- data.frame( country = rep("US", 2L), year = rep("2020", 2L), variable = rep("tptinc992j", 2L), percentile = c("p90", "p10"), value = c(200000, 10000), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_percentile_ratio(d)d <- data.frame( country = rep("US", 2L), year = rep("2020", 2L), variable = rep("tptinc992j", 2L), percentile = c("p90", "p10"), value = c(200000, 10000), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_percentile_ratio(d)
Enumerated from the pXpY grammar: key groups, deciles, centiles, and top-1\
wid_percentiles wid_percentileswid_percentiles wid_percentiles
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Cross-country comparison bar/point chart
wid_plot_compare( data, year = NULL, variable = NULL, country = NULL, country_labels = NULL, type = "bar" )wid_plot_compare( data, year = NULL, variable = NULL, country = NULL, country_labels = NULL, type = "bar" )
data |
A |
year |
Integer year (defaults to most recent common year). |
variable |
Variable code to plot. |
country |
Country codes to include. |
country_labels |
Named character vector for renaming countries. |
type |
|
A ggplot object.
d <- data.frame( country = rep(c("US", "FR"), each = 3L), year = rep(c("2018", "2019", "2020"), 2L), variable = "sptinc992j", percentile = "p99p100", value = c(0.20, 0.19, 0.19, 0.11, 0.11, 0.12), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_compare(d, year = 2020)d <- data.frame( country = rep(c("US", "FR"), each = 3L), year = rep(c("2018", "2019", "2020"), 2L), variable = "sptinc992j", percentile = "p99p100", value = c(0.20, 0.19, 0.19, 0.11, 0.11, 0.12), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_compare(d, year = 2020)
Lorenz curve
wid_plot_lorenz(data, variable = NULL, country = NULL, country_labels = NULL)wid_plot_lorenz(data, variable = NULL, country = NULL, country_labels = NULL)
data |
A |
variable |
Variable code to plot. |
country |
Country codes to include. |
country_labels |
Named character vector for renaming countries. |
A ggplot object.
d <- data.frame( country = rep("US", 3L), year = rep("2020", 3L), variable = "sptinc992j", percentile = c("p0p50", "p50p90", "p90p100"), value = c(0.20, 0.40, 0.40), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_lorenz(d)d <- data.frame( country = rep("US", 3L), year = rep("2020", 3L), variable = "sptinc992j", percentile = c("p0p50", "p50p90", "p90p100"), value = c(0.20, 0.40, 0.40), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_lorenz(d)
Time-series line chart for WID data
wid_plot_timeseries( data, variable = NULL, country = NULL, country_labels = NULL, facet = FALSE )wid_plot_timeseries( data, variable = NULL, country = NULL, country_labels = NULL, facet = FALSE )
data |
A |
variable |
Variable code to plot. |
country |
Country codes to include. |
country_labels |
Named character vector for renaming countries. |
facet |
Logical. Facet by country. Default |
A ggplot object.
d <- data.frame( country = rep(c("US", "FR"), each = 3L), year = rep(c("2018", "2019", "2020"), 2L), variable = "sptinc992j", percentile = "p99p100", value = c(0.20, 0.19, 0.19, 0.11, 0.11, 0.12), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_timeseries(d)d <- data.frame( country = rep(c("US", "FR"), each = 3L), year = rep(c("2018", "2019", "2020"), 2L), variable = "sptinc992j", percentile = "p99p100", value = c(0.20, 0.19, 0.19, 0.11, 0.11, 0.12), age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_plot_timeseries(d)
Population unit codes (\S2.1.4)
WID population unit codes
wid_pop_types wid_pop_typeswid_pop_types wid_pop_types
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Creates a wid_query object that stores query parameters for WID data fetching.
wid_query(...)wid_query(...)
... |
Named arguments matching |
A wid_query object (a named list with class "wid_query").
q <- wid_query(indicators = "sptinc992j", areas = "US", years = 2010:2020)q <- wid_query(indicators = "sptinc992j", areas = "US", years = 2010:2020)
Search WID lookup tables by regex
wid_search(query, tables = "concepts", type = NULL)wid_search(query, tables = "concepts", type = NULL)
query |
Regular expression. |
tables |
Table names to search. Default |
type |
Optional series-type letter to filter results. |
A data.frame with columns table,
variable, and description for each matching row,
or an invisible empty data.frame if no matches are found.
wid_search("national income") wid_search("wealth", type = "s") wid_search("^US", tables = "countries")wid_search("national income") wid_search("wealth", type = "s") wid_search("^US", tables = "countries")
Series type codes (\S2.1.1)
WID series type codes
wid_series_types wid_series_typeswid_series_types wid_series_types
data.frame(code, description)
A data frame with columns code and description.
https://wid.world/codes-dictionary/
https://wid.world/codes-dictionary/
Set WID API key
wid_set_key(key)wid_set_key(key)
key |
character API key |
Invisibly, the key string.
Tidy a wid_df: decode variable codes and coerce column types
wid_tidy(data, decode = TRUE, country_names = TRUE)wid_tidy(data, decode = TRUE, country_names = TRUE)
data |
A |
decode |
Logical. Add decoded columns. Default |
country_names |
Logical. Join country display names. Default |
A plain data.frame with year as integer, value
as double, and optionally indicator, series_type,
type_label, and country_name columns appended.
d <- data.frame( country = "US", variable = "sptinc992j", percentile = "p99p100", year = "2020", value = "0.19", age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_tidy(d, country_names = FALSE)d <- data.frame( country = "US", variable = "sptinc992j", percentile = "p99p100", year = "2020", value = "0.19", age = "992", pop = "j", stringsAsFactors = FALSE ) class(d) <- c("wid_df", "data.frame") wid_tidy(d, country_names = FALSE)
Validate WID code components
wid_validate( series_type = NULL, concept = NULL, age = NULL, pop = NULL, years = NULL, areas = NULL, perc = NULL )wid_validate( series_type = NULL, concept = NULL, age = NULL, pop = NULL, years = NULL, areas = NULL, perc = NULL )
series_type |
One-letter series type code. |
concept |
5-6 letter concept code. |
age |
Three-digit age code. |
pop |
One-letter population unit code. |
years |
Integer vector of years. |
areas |
ISO-2 area codes. |
perc |
Percentile codes of the form |
Invisibly, a named list of normalised inputs with the same names as
the parameters, with age zero-padded and years coerced to
integer.
wid_validate(series_type = "s", concept = "ptinc", age = 992, pop = "j") wid_validate(areas = c("US", "FR", "US-CA"))wid_validate(series_type = "s", concept = "ptinc", age = 992, pop = "j") wid_validate(areas = c("US", "FR", "US-CA"))