Simple R package for UN Comtrade API

uncomtrade-logo.pngI have recently been doing a lot of work in R, a statistical programming language, and have started accumulating a small collection of functions that I regularly use. One of the tools I have particularly come to rely on is this function to obtain data from the United Nation’s Comtrade database, a great source for country-to-country trade statistics. Since there seemed to be no R package that provides access to their new API (which is still in beta), I simply wrote a function for myself based on the sample code on their homepage and kept it with my personal files. Motivated by posts like this, however, I decided to put the code into an R package and share it here for others to use.

You can get the source file here: comtrade.tar.gz, MD5 sum: 22ce86b845d8a8ec7af98b0377c5f885

The package is identical to the sample code on the UN homepage and simply adds documentation. Note that they say that their API may change at any point, and since I have no plans to keep this updated (for now, at least), this may simply stop working in the future. Nevertheless, here is how to set up the package directly from within R:

download.file(url = "https://media.portblue.net/resources/uncomtrade-r-package/comtrade.tar.gz", destfile = "comtrade.tar.gz")
library(tools)
md5sum("comtrade.tar.gz")

If the MD5 checksum corresponds to what you see above, go ahead and install the file and start pulling data from the UN using the get.Comtrade() function call:

install.packages("comtrade.tar.gz", repos = NULL, type = "source")
library(comtrade)
(s1 <- get.Comtrade(r = "842", p = "124,484"))

9 comments to Simple R package for UN Comtrade API

  • Guillermo

    Hi! One question (sorry if it is too basic): once the data is extracted, how can I get a data frame out to it? Or, how can I save it as a CSV file? Thank you very much. Best, Guillermo

  • Kush

    Thanks for writing this package. I could not export s1 to a csv file using write.csv. It would be great if you could a couple of lines that show to either convert s1 to a dataframe or to export it as a csv file

    write.csv(s1)
    Error in data.frame(validation = NULL, data = list(Classification = c(1L, :
    arguments imply differing number of rows: 0, 6

    Thanks!

  • Xiaofeng

    Thank you very much for your work! How can I get the hs6 product-based data (six digits) data from get.Comtrade()?

    Specifically, I want to get the product (hs6 code: 880211) trade data between 842 (partner) and 642 (reporter). But I cannot an error from get.Comtrade(r = “642″, p = “842″, px=”880211″).

    How can I get the data?

    Thanks !

    • stefan.a

      Hi Xiaofeng! As I wrote, I simply just copied the sample code from UNCTAD and packaged it, so I’d suggest you check out their homepage for some example calls to their API. I think you have to use the parameter “px” to specify the classification code you want to use (e.g. “H4″ for HS 2012), and the parameter “cc” to supply the specific product code you’re looking for, but I haven’t had time to try this myself. Have a look at this website for a more detailed explanation of what each parameter value stands for.

      Good luck!

  • Meghna

    Hi Stefan,
    Thank you for this! It is very useful! I just have one question. I am trying to extract data for all HS6 commodities exported from all countries to the rest of the world for the year 1992. However, even using your package I get a return that says “Result too large” My requested result set count is 399004. Is there a way for me to define the country ids into 4/5 sets and get the results for each set separately?
    Thank you for your time!

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>