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].
To improve the presentation of the tables I shall use DT
and the datatable()
function besides tradestatistics
.
Provided that this package obtains data from an API, it is useful to know which tables can be accessed:
You might notice the tables have a pattern. The letters indicate the presence of columns that account for the level of detail in the data:
y
: year column.r
: reporter columnp
: partner columnc
: product code columnThe most aggregated table is yr
which basically says how many dollars each country exports and imports for a given year.
The less aggregated table is yrpc
which says how many dollars of each of the 1,222 products from the Harmonized System each country exports to other countries and imports from other countries.
For the complete detail you can check docs.tradestatistics.io.
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:
The Package Functions section explains that you don’t need to memorize all HS codes. The functions within this package are designed to match strings (i.e. “apple”) to valid HS codes (i.e. “0808”).
This table is provided to be used with ots_inflation_adjustment()
.
The end user can use this function to find an ISO code by providing a country name. This works by implementing partial search.
Basic examples:
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(... reporters = "Chile" ...)
and it will automatically replace your input for a valid ISO in case there is a match. This will be covered in detail in the Trade Data section.
The end user can use this function to find a product code by providing a product name. This works by implementing partial string matching:
This function searches both products and groups:
The end user can use this function to find a community code by providing a community name. This works by implementing partial string matching:
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.
If we want Chile-Argentina bilateral trade at community level in 1962:
yrpc <- ots_create_tidy_data(
years = 1962,
reporters = "chl",
partners = "arg",
table = "yrpc"
)
datatable(yrpc)
We can pass two years or more, several reporters/partners, and filter by community with exact codes or code matching based on keywords:
# Note that here I'm passing Peru and not per which is the ISO code for Peru
# The same applies to Brazil
yrpc2 <- ots_create_tidy_data(
years = c(1962,1963),
reporters = c("chl", "Peru", "bol"),
partners = c("arg", "Brazil"),
sections = c("01", "food"),
table = "yrpc"
)
#> Warning in ots_create_tidy_data_unmemoised(...): The sections argument will be
#> ignored provided that you requested a table without section_code field.
datatable(yrpc2)
yrpc
table returns some fields that deserve an explanation which can be seen at docs.tradestatistics.io.
If we want Chile-Argentina bilateral trade at aggregated level in 1962 and 1963:
yrp <- ots_create_tidy_data(
years = 1962:1963,
reporters = c("chl", "per"),
partners = "arg",
table = "yrp"
)
datatable(yrp)
This table accepts different years, reporters and partners just like yrpc
.
If we want Chilean trade at product level in 1962 with respect to product 0101
which means “Horses, asses, mules and hinnies; live”:
yrc <- ots_create_tidy_data(
years = 1962,
reporters = "chl",
products = "0101",
table = "yrc"
)
datatable(yrc)
This table accepts different years, reporters and product codes just like yrpc
.
All the variables from this table are documented at docs.tradestatistics.io.
If we want the aggregated trade of Chile, Argentina and Peru in 1962 and 1963:
yr <- ots_create_tidy_data(
years = 1962:1963,
reporters = c("chl", "arg", "per"),
table = "yr"
)
datatable(yr)
This table accepts different years and reporters just like yrpc
.
All the variables from this table are documented at docs.tradestatistics.io.
If we want all products traded in 1962:
If we want the traded values of the product 0101
which means "Horses, asses, mules and hinnies; live: in 1962:
This table accepts different years just like yrpc
.
Taking the yr
table from above, we can use ots_inflation_adjustment()
to convert dollars from 1962 to dollars of 1970: