inegiR is a package designed to interact with the two API’s of INEGI (Oficial statistics agency of Mexico). Because these work with JSON or XML formating, this package is essentially a wrapper for jsonlite, XML and some tidy plyr transformations.
The package uses two main functions:
serie_inegi()- Used to query a data series from INEGI.
denue_inegi()- Used to query the information in the DENUE database.
The remaining functions serve as elegant wrappers to perform common tasks. For example
inflacion_general() to download monthly inflation data. Other functions make transformations easier on-the-fly, such as
YoY() to calculate a percentage change from a year ago (year-over-year).
Example 1: downloading a data series
To get the CRAN version (as of Nov-2015):
To download dev version on github, using devtools:
There are roughly two ways to download data series: the “general” and the “short” way (provided there is a wrapper function available).
In the first case, the function parses a URL provided by the user. All the URL’s for each data series can be found in the INEGI development site. You must also sign up for an API token in that same site with your email.
Let us save the imaginary token:
Now, I wish to find the rate of inflation (which in the case of INEGI is a percent change of the INPC data series).
This is the corresponding URL for INPC data.series:
JSON format is also accepted and is interchangeable (do not use the “?callback?” sign provided by INEGI’s documentation):
Now, we are going to download this data series as a data.frame.
The optional “metadata” parameter in serie_inegi allows us to also download the metadata information from the data series, which includes date of update, units, frequency, etc.
If “metadata” is set to TRUE, the information is parsed as a list of two elements: the metadata and the data frame.
To access any of these elements, simply use as a list:
Now that we have the INPC data series, we must apply a year-over-year change. For this we use the handy YoY() function, which let’s us choose the amount of periods to compare over (12 if you want year over year for a monthly series):
This method works for any URL obtained from the INEGI documentation, but for the most used indicators, the package has built-in shortcut wrappers.
Let us obtain the same data series (inflation) via one of these specified shortcut functions:
Example 2: downloading statistics from DENUE
The DENUE is a directory of businesses in Mexico and is accesible by another API within INEGI here. A different API token is used for these queries.
To download the businesses in a certain radius, we need a few coordinates. Let’s use the ones around Monterrey Mexico’s main square:
Now, we download into a data.frame the list of businesses in a 250 meter radius.
Let’s see only the first rows and columns…
If you would like to change some parameters, this is accepted. For example a 1km radius and only businesses with “Restaurante” in the description.