Skip to contents

fetch_indicator() plus country_join() in one step: pull an indicator from any registered source and attach it to a frame you already have, matched on the ISO spine (and on year too, when both sides are panels).

Usage

add_indicator(data, source, indicator, countries = NULL, years = NULL, ...)

Arguments

data

A frame with iso3c (or a country column join_world() would recognise).

source, indicator, countries, years, ...

Passed to fetch_indicator(). countries defaults to the codes already in data, so you never have to restate them. How much that saves depends on the source – see below.

Value

data with the indicator column(s) added.

How much countries actually saves

countries bounds the result, not necessarily the download. Only some providers accept a country filter in the request:

Sourcecountries reaches the provider?
comtradeyes – sent as reporter
wdipartly – the year range is sent, countries are filtered here
owid, eurostat, oecdno – the full dataset is downloaded, then filtered

So add_indicator(one_row, "owid", "life-expectancy") still transfers every country and year that dataset holds in order to keep a single value. When that matters, narrow with years (which the wdi and comtrade adapters do push down), or fetch once into a variable and reuse it rather than calling this per subset. The on-disk cache means a repeated wdi fetch is free; the other sources are memoised for the session only.

Examples

if (FALSE) { # \dontrun{
world_snapshot$countries |>
  add_indicator("wdi", c(unemployment = "SL.UEM.TOTL.ZS"), years = 2020)
} # }