Basic usage

knitr::opts_chunk$set(
  cache = FALSE,
  collapse = TRUE,
  message = FALSE,
  comment = "#>"
)

Introduction

This vignette explains the functions within this package. The idea is to show how this package simplifies obtaining data from (api.tradestatistics.io)[https://api.tradestatistics.io].

library(tradestatistics)

Package data

Available tables

Provided that this package obtains data from an API, it is useful to know which tables can be accessed:

ots_tables

The ITPD-E table means "International Trade and Production Database for Estimation" and does not contain imputed values.

The ITPD-S table means "International Trade and Production Database for Simulation" and contains imputed values using the gravity model of trade.

The most aggregated tables are itpde_imp/itpds_imp (for imports, or with _exp for exporter) which contains how many dollars each country imports and exports in a year.

The less aggregated tables are itpde/itpds which detail bilateral trade at industry level in a year.

For the complete detail you can check tradestatistics.io.

Country codes

The Package Functions section explains that you don't need to memorize all ISO codes. The functions within this package are designed to match strings (i.e. "United States" or "America") to valid ISO codes (i.e. "USA").

Just as a reference, the table with all valid ISO codes can be accessed by running this:

ots_countries

Sector/industry codes

The functions within this package are designed to match strings (i.e. "agriculture") to sector/industry codes.

ots_sectors

ots_industries

Package functions

Country code

The end user can use this function to find a dynamic ISO-3 code by providing a country name. This works by implementing partial search.

Basic examples:

# Single match with no replacement
ots_country_code("Chile")

# Single match with replacement
ots_country_code("America")

# Double match with no replacement
ots_country_code("Germany")

The function ots_country_code() is used by ots_create_tidy_data() in a way that you can pass parameters like ots_create_tidy_data(... importers = "Chile" ...) and it will automatically replace your input for a valid code in case there is a match. This will be covered in detail in the Trade Data section.

Sector/industry code

The end user can find a code or a set of codes by looking for keywords for commodities or groups. The function ots_commodity_code() allows to search from the official commodities and groups in the Harmonized system:

ots_sector_code(" AgriCulture ")
ots_industry_code("  CeReaLS")

Trade data

This function downloads data for a single year and needs (at least) some filter parameters according to the query type.

Here we cover aggregated tables to describe the usage.

Bilateral trade (aggregated)

If we want Chile-Argentina bilateral trade at community level in 2019:

ots_create_tidy_data(
  years = 2019,
  importers = "chl",
  exporters = "arg",
  table = "itpde_imp_exp"
)

We can pass two years or more, several reporters/partners, and filter by commodities with exact codes or code matching based on keywords:

ots_create_tidy_data(
  years = 2018:2019,
  importers = c("chl", "Peru", "col"),
  exporters = c("arg", "Brazil"),
  table = "itpde_imp_exp"
)

Bilateral trade (sector)

ots_create_tidy_data(
  years = 2018:2019,
  importers = c("chl", "Peru", "col"),
  exporters = c("arg", "Brazil"),
  table = "itpde_imp_exp_sec"
)

Aggregate trade (industry)

ots_create_tidy_data(
  years = 2018:2019,
  importers = c("chl", "Peru", "col"),
  exporters = c("arg", "Brazil"),
  table = "itpde_ind"
)


Try the tradestatistics package in your browser

Any scripts or data that you put into this service are public.

tradestatistics documentation built on July 21, 2026, 5:09 p.m.