| Title: | Access and Analyse UN Comtrade International Trade Data |
|---|---|
| Description: | Download and analyse international merchandise and services trade data from the United Nations Comtrade database <https://comtradeplus.un.org/>. Retrieve bilateral trade flows, compute trade analytics (revealed comparative advantage, trade concentration, trade balance), and convert between commodity classifications (Harmonised System 'HS', Standard International Trade Classification 'SITC', Broad Economic Categories 'BEC'). Covers 200+ reporter countries, 60+ years of goods trade data (1962-present), and services trade via Extended Balance of Payments Services ('EBOPS'). Works without registration for basic queries. A free Application Programming Interface ('API') key from <https://comtradedeveloper.un.org/> unlocks full access. |
| Authors: | Charles Coverdale [aut, cre] |
| Maintainer: | Charles Coverdale <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-06-05 06:28:28 UTC |
| Source: | https://github.com/charlescoverdale/comtrade |
Check which years and classifications have data available for a given reporter country.
ct_available(reporter, cache = TRUE)ct_available(reporter, cache = TRUE)
reporter |
Character. Reporter country ISO3 code. |
cache |
Logical. Cache results. Default |
A data.frame with columns: year, classification, type (C/S), frequency (A/M).
op <- options(comtrade.cache_dir = tempdir()) ct_available("GBR") options(op)op <- options(comtrade.cache_dir = tempdir()) ct_available("GBR") options(op)
Compute the trade balance (exports minus imports) for a reporter country against each trading partner.
ct_balance( reporter, partner = "0", year = NULL, commodity = "TOTAL", cache = TRUE )ct_balance( reporter, partner = "0", year = NULL, commodity = "TOTAL", cache = TRUE )
reporter |
Character. Reporter country ISO3 code. |
partner |
Character. Partner country code, or |
year |
Integer. Year(s) to query. |
commodity |
Character. Commodity code. Default |
cache |
Logical. Default |
A data.frame with columns: partner, partner_desc, year, exports, imports, balance.
op <- options(comtrade.cache_dir = tempdir()) ct_balance("GBR", year = 2023) options(op)op <- options(comtrade.cache_dir = tempdir()) ct_balance("GBR", year = 2023) options(op)
Remove all cached Comtrade API responses from the local cache directory.
ct_cache_clear()ct_cache_clear()
Invisibly returns TRUE if the cache was cleared, FALSE if
no cache directory existed.
op <- options(comtrade.cache_dir = tempdir()) ct_cache_clear() options(op)op <- options(comtrade.cache_dir = tempdir()) ct_cache_clear() options(op)
Search the HS (Harmonized System) commodity classification for codes matching a keyword or code pattern. Uses a built-in table of 96 two-digit HS chapters with descriptions.
ct_commodities(query = NULL, level = NULL)ct_commodities(query = NULL, level = NULL)
query |
Character. Search term (matched against commodity
descriptions) or a partial HS code (e.g., |
level |
Integer. HS digit level to return. Currently only level 2
is available from the built-in table. Default |
A data.frame with columns:
HS 2-digit chapter code
Chapter description
Digit level (always 2 for built-in table)
Parent code (NA for 2-digit chapters)
# Search for petroleum-related codes ct_commodities("petroleum") # List all 2-digit HS chapters ct_commodities() # Find codes starting with "27" (mineral fuels) ct_commodities("27")# Search for petroleum-related codes ct_commodities("petroleum") # List all 2-digit HS chapters ct_commodities() # Find codes starting with "27" (mineral fuels) ct_commodities("27")
Compare multiple countries' trade in a given commodity, showing exports, imports, balance, and revealed comparative advantage.
ct_compare(reporters, commodity = "TOTAL", year = NULL, cache = TRUE)ct_compare(reporters, commodity = "TOTAL", year = NULL, cache = TRUE)
reporters |
Character vector. ISO3 codes for countries to compare. |
commodity |
Character. Commodity code. Default |
year |
Integer. Year to query. |
cache |
Logical. Default |
A data.frame with columns: reporter, reporter_desc, exports, imports, balance, rca.
op <- options(comtrade.cache_dir = tempdir()) ct_compare(c("GBR", "DEU", "FRA"), commodity = "87", year = 2023) options(op)op <- options(comtrade.cache_dir = tempdir()) ct_compare(c("GBR", "DEU", "FRA"), commodity = "87", year = 2023) options(op)
Convert commodity codes between HS (Harmonized System), SITC, and BEC classifications. Uses a built-in concordance table covering the most common 2-digit HS chapters mapped to SITC sections and BEC categories.
ct_concordance(code, from = "HS", to = "SITC")ct_concordance(code, from = "HS", to = "SITC")
code |
Character. The commodity code(s) to convert. |
from |
Character. Source classification: |
to |
Character. Target classification: |
For detailed 4/6-digit concordances, see the concordance R package
on CRAN.
A data.frame with columns: from_code, from_desc, to_code, to_desc.
# Convert HS chapter 27 (mineral fuels) to SITC ct_concordance("27", from = "HS", to = "SITC") # Convert SITC section 0 (food) to HS ct_concordance("0", from = "SITC", to = "HS")# Convert HS chapter 27 (mineral fuels) to SITC ct_concordance("27", from = "HS", to = "SITC") # Convert SITC section 0 (food) to HS ct_concordance("0", from = "SITC", to = "HS")
Compute year-on-year and cumulative trade growth for a bilateral flow.
ct_growth( reporter, partner = "0", commodity = "TOTAL", flow = "X", years = NULL, cache = TRUE )ct_growth( reporter, partner = "0", commodity = "TOTAL", flow = "X", years = NULL, cache = TRUE )
reporter |
Character. Reporter country ISO3 code. |
partner |
Character. Partner country code. Default |
commodity |
Character. Commodity code. Default |
flow |
Character. |
years |
Integer vector. Years to query (at least 2). |
cache |
Logical. Default |
A data.frame with columns: year, value, growth_yoy, growth_cumulative, index_100.
op <- options(comtrade.cache_dir = tempdir()) ct_growth("GBR", flow = "X", years = 2018:2023) options(op)op <- options(comtrade.cache_dir = tempdir()) ct_growth("GBR", flow = "X", years = 2018:2023) options(op)
Compute the Herfindahl-Hirschman Index measuring the concentration of a country's trade across partners or products.
ct_hhi( reporter, flow = "X", year = NULL, by = "partner", level = 2L, cache = TRUE )ct_hhi( reporter, flow = "X", year = NULL, by = "partner", level = 2L, cache = TRUE )
reporter |
Character. Reporter country ISO3 code. |
flow |
Character. |
year |
Integer. Year to query. |
by |
Character. Concentrate by |
level |
Integer. HS digit level (only used when |
cache |
Logical. Default |
HHI ranges from 0 (perfectly diversified) to 10,000 (single partner/product). Interpretation: < 1,500 = low concentration, 1,500-2,500 = moderate, > 2,500 = high.
A data.frame with columns: year, hhi, concentration, n_items, top_item, top_share_pct.
op <- options(comtrade.cache_dir = tempdir()) # Export partner concentration ct_hhi("AUS", flow = "X", year = 2023, by = "partner") # Export product concentration ct_hhi("AUS", flow = "X", year = 2023, by = "product") options(op)op <- options(comtrade.cache_dir = tempdir()) # Export partner concentration ct_hhi("AUS", flow = "X", year = 2023, by = "partner") # Export product concentration ct_hhi("AUS", flow = "X", year = 2023, by = "product") options(op)
Compute the Revealed Comparative Advantage for a country's exports. RCA > 1 indicates the country has a comparative advantage in that product.
ct_rca(reporter, year = NULL, level = 2L, cache = TRUE)ct_rca(reporter, year = NULL, level = 2L, cache = TRUE)
reporter |
Character. Reporter country ISO3 code. |
year |
Integer. Year to query. |
level |
Integer. HS digit level: 2, 4, or 6. Default 2. |
cache |
Logical. Default |
The Balassa index is defined as: RCA = (country exports of product i / country total exports) / (world exports of product i / world total exports)
A data.frame with columns: commodity_code, commodity_desc, reporter_value, world_value, reporter_share, world_share, rca, has_advantage.
op <- options(comtrade.cache_dir = tempdir()) rca <- ct_rca("AUS", year = 2023) # Products where Australia has comparative advantage rca[rca$has_advantage, ] options(op)op <- options(comtrade.cache_dir = tempdir()) rca <- ct_rca("AUS", year = 2023) # Products where Australia has comparative advantage rca[rca$has_advantage, ] options(op)
Get the list of countries that report trade data to UN Comtrade, with their ISO3 codes and M49 numeric codes.
ct_reporters(cache = TRUE)ct_reporters(cache = TRUE)
cache |
Logical. Cache the reference table locally. Default |
A data.frame with columns:
M49 numeric country code (used in API queries)
ISO 3166-1 alpha-3 code (e.g., GBR, USA, AUS)
Country name
Logical. TRUE for country groups (e.g., EU, OECD)
op <- options(comtrade.cache_dir = tempdir()) reporters <- ct_reporters() head(reporters) options(op)op <- options(comtrade.cache_dir = tempdir()) reporters <- ct_reporters() head(reporters) options(op)
Download international services trade data from the UN Comtrade database using the EBOPS (Extended Balance of Payments Services) classification.
ct_services( reporter, partner = "0", service = "TOTAL", flow = c("X", "M"), year = NULL, cache = TRUE )ct_services( reporter, partner = "0", service = "TOTAL", flow = c("X", "M"), year = NULL, cache = TRUE )
reporter |
Character. Reporter country ISO3 code. |
partner |
Character. Partner country code. Default |
service |
Character. EBOPS service code. Default |
flow |
Character. Trade flow: |
year |
Integer. Year(s) to query (2000-present). Default: most recent available year. |
cache |
Logical. Cache results locally. Default |
A data.frame with columns: reporter, reporter_desc, partner, partner_desc, flow, flow_desc, service_code, service_desc, year, trade_value_usd.
op <- options(comtrade.cache_dir = tempdir()) # UK services exports to the world ct_services("GBR", year = 2022, flow = "X") options(op)op <- options(comtrade.cache_dir = tempdir()) # UK services exports to the world ct_services("GBR", year = 2022, flow = "X") options(op)
Store your UN Comtrade API key for the current session. The key is saved as an R option and optionally as an environment variable for persistence.
ct_set_key(key, install = FALSE)ct_set_key(key, install = FALSE)
key |
Character. Your Comtrade API subscription key. |
install |
Logical. If |
Get a free API key at https://comtradedeveloper.un.org/. The free tier allows 500 calls per day and up to 100,000 records per call.
Invisibly returns the key.
ct_set_key("your-subscription-key-here")ct_set_key("your-subscription-key-here")
Rank a country's trading partners by total trade value.
ct_top_partners(reporter, flow = "X", year = NULL, n = 20L, cache = TRUE)ct_top_partners(reporter, flow = "X", year = NULL, n = 20L, cache = TRUE)
reporter |
Character. Reporter country ISO3 code. |
flow |
Character. |
year |
Integer. Year to query. |
n |
Integer. Number of top partners to return. Default 20. |
cache |
Logical. Default |
A data.frame with columns: partner, partner_desc, value, share_pct, rank.
op <- options(comtrade.cache_dir = tempdir()) ct_top_partners("GBR", flow = "X", year = 2023) options(op)op <- options(comtrade.cache_dir = tempdir()) ct_top_partners("GBR", flow = "X", year = 2023) options(op)
Rank a country's traded products by value, showing the top N with percentage shares.
ct_top_products( reporter, flow = "X", year = NULL, n = 20L, level = 2L, cache = TRUE )ct_top_products( reporter, flow = "X", year = NULL, n = 20L, level = 2L, cache = TRUE )
reporter |
Character. Reporter country ISO3 code. |
flow |
Character. |
year |
Integer. Year to query. |
n |
Integer. Number of top products to return. Default 20. |
level |
Integer. HS digit level: 2, 4, or 6. Default 2. |
cache |
Logical. Default |
A data.frame with columns: commodity_code, commodity_desc, value, share_pct, rank.
op <- options(comtrade.cache_dir = tempdir()) ct_top_products("AUS", flow = "X", year = 2023) options(op)op <- options(comtrade.cache_dir = tempdir()) ct_top_products("AUS", flow = "X", year = 2023) options(op)
Download merchandise trade data from the UN Comtrade database. Returns bilateral trade flows between reporter and partner countries, optionally filtered by commodity code and trade flow direction.
ct_trade( reporter, partner = "0", commodity = "TOTAL", flow = c("X", "M"), year = NULL, frequency = "A", classification = "HS", cache = TRUE )ct_trade( reporter, partner = "0", commodity = "TOTAL", flow = c("X", "M"), year = NULL, frequency = "A", classification = "HS", cache = TRUE )
reporter |
Character. Reporter country ISO3 code (e.g., |
partner |
Character. Partner country ISO3 code, or |
commodity |
Character. HS commodity code(s). |
flow |
Character. Trade flow: |
year |
Integer. Year(s) to query (1962-present). Can be a vector. Maximum 12-year span per query on the free tier. Default: most recent available year. |
frequency |
Character. |
classification |
Character. Commodity classification system. Default
|
cache |
Logical. Cache results locally for 24 hours. Default |
A data.frame with columns:
Reporter country code
Reporter country name
Partner country code
Partner country name
Trade flow code (X, M, RX, RM)
Trade flow description
Commodity code
Commodity description
Reference year
Reference period (year or year-month)
Trade value in US dollars
Net weight in kilograms
Quantity in supplementary units
Supplementary quantity unit
op <- options(comtrade.cache_dir = tempdir()) # UK total exports to the world, 2023 ct_trade("GBR", year = 2023, flow = "X") # US imports of crude petroleum from Saudi Arabia ct_trade("USA", partner = "SAU", commodity = "2709", flow = "M", year = 2020:2023) # Australia's top-level trade with China ct_trade("AUS", partner = "CHN", year = 2023) options(op)op <- options(comtrade.cache_dir = tempdir()) # UK total exports to the world, 2023 ct_trade("GBR", year = 2023, flow = "X") # US imports of crude petroleum from Saudi Arabia ct_trade("USA", partner = "SAU", commodity = "2709", flow = "M", year = 2020:2023) # Australia's top-level trade with China ct_trade("AUS", partner = "CHN", year = 2023) options(op)