<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/inegir-v2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.enelmargen.org/datascience/inegiR-v2/"> En El Margen - R-English</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>After <em>a lot</em> of slacking around, I finally got to finishing the upgraded version of the <strong>inegiR</strong> package on CRAN. This version combines quite a few changes that I will explain further in this post.</p>
<h2>New language</h2>
<p>The biggest change upfront is the migration to english in both function names and documentation. The rationale behind this is to make it more accessible to developers around the world (I have recieved a few emails asking for translations). Also, the non-ASCII characters were not helpful. For the Mexican users, I assume that if you know R, you can probably find yourself around an english document.</p>
<p>To avoid crashing workflows, I left the legacy functions intact except for a warning to use the english version instead. An example of this is the commercial growth rate functions, which are:</p>
<figure>
<pre><code data-lang="r"># english
rate_commerce()
# spanish (old version)
tasa_comercio()</code></pre>
</figure>
<h2>Route API</h2>
<p>With some <a rel="nofollow" href="https://github.com/Eflores89/inegiR/issues/13" target="_blank">help</a> from Arturo Cárdenas and a revamp of the <a rel="nofollow" href="http://www.inegi.org.mx/desarrolladores/sakbe/apisakbe.aspx" target="_blank">Sákbe API</a> in INEGI, I was able to add functions to access route information.</p>
<p>The two main ones are:</p>
<figure>
<pre><code data-lang="r"># to search for a destiny id
inegi_destiny()
# to get route information
inegi_route()</code></pre>
</figure>
<p>The first thing to understand is that INEGI has categorized sites in Mexico according to a “destiny id”. For example, the International Airport in Mexico city is destiny id #57. The inegi_destiny() function will help you find a destiny id based on a text criteria, sort of like googling the place and getting an address. Here is an example with a plaza in Monterrey:</p>
<figure>
<pre><code data-lang="r"># download on CRAN or newest dev version (if not accepted yet)
# install.packages(&quot;inegiR&quot;)
# or... 
# devtools::install_github(&quot;eflores89/inegiR&quot;)
library(inegiR)
library(knitr)
# to search for Macroplaza destiny id
token &lt;- &quot;mytoken&quot;
destiny1 &lt;- inegi_destiny(&quot;Macroplaza&quot;, token = token)
kable(destiny1)</code></pre>
</figure>
<table>
<thead>
<tr>
<th>ID</th>
<th>ID_DEST</th>
<th>STATE</th>
<th>NAME</th>
<th>GEO_STRING</th>
<th>TYPE</th>
<th>LAT</th>
<th>LONG</th>
</tr>
</thead>
<tbody>
<tr>
<td>destino</td>
<td>6940</td>
<td>N.L.</td>
<td>Macroplaza, Monterrey</td>
<td>{“type”:”Point”,”coordinates”:[-100.309991587,25.668862054]}</td>
<td>Point</td>
<td>-100.3100</td>
<td>25.66886</td>
</tr>
<tr>
<td>destino</td>
<td>20237</td>
<td>B.C.</td>
<td>Macroplaza del Valle, Mexicali</td>
<td>{“type”:”Point”,”coordinates”:[-115.50790804,32.62128025]}</td>
<td>Point</td>
<td>-115.5079</td>
<td>32.62128</td>
</tr>
<tr>
<td>destino</td>
<td>17891</td>
<td>Coah.</td>
<td>Macroplaza, Acuña</td>
<td>{“type”:”Point”,”coordinates”:[-100.978421457,29.3299882860001]}</td>
<td>Point</td>
<td>-100.9784</td>
<td>29.32999</td>
</tr>
</tbody>
</table>
<p>When you know two destiny id’s, you can now use the API to learn about potential routes you can take between them. This function will return a list with two objects: a data.frame of route information (kilometers, toll cost, etc) and another data.frame with all the coordinates in the route. Intuitively, if you join all the dots, you can clearly see the route you would take.</p>
<p>To illustrate, i’m going to use the first result and see what the route would be from there to the U.S. Border (which is the other id) with a normal car and with a tolled highway. A further look at the documentation will explain the names and options in the parameters.</p>
<figure>
<pre><code data-lang="r">route &lt;- inegi_route(from = 6940, to = 7426, token = token, pref = 1, vehicle = 1)
str(route)
# List of 2
#  $ ROUTE          :'data.frame':	1 obs. of  6 variables:
#   ..$ KMS       : num 222
#   ..$ TIME_MINS : num 151
#   ..$ TIME_HRS  : num 2.52
#   ..$ HAS_TOLL  : logi TRUE
#   ..$ TOLL_COST : num 364
#   ..$ TOTAL_COST: logi NA
#  $ COORDINATE_PATH:'data.frame':	1176 obs. of  2 variables:
#   ..$ V1: num [1:1176] -100 -100 -100 -100 -100 ...
#   ..$ V2: num [1:1176] 25.7 25.7 25.7 25.7 25.7 ...</code></pre>
</figure>
<p>As you can see, the returning element is a list of two data.frame objects. The first will give us basic statistics about the route.</p>
<figure>
<pre><code data-lang="r">kable(route$ROUTE)</code></pre>
</figure>
<table>
<thead>
<tr>
<th>KMS</th>
<th>TIME_MINS</th>
<th>TIME_HRS</th>
<th>HAS_TOLL</th>
<th>TOLL_COST</th>
<th>TOTAL_COST</th>
</tr>
</thead>
<tbody>
<tr>
<td>222.36</td>
<td>151.11</td>
<td>2.5185</td>
<td>TRUE</td>
<td>364</td>
<td>NA</td>
</tr>
</tbody>
</table>
<p>The total cost is NA because the default value for the <em>calc_cost</em> parameter is FALSE. When this is set to TRUE, the function will additionally look for the price of gasoline in the Sakbé API and calculate a cost of the trip. Be warned, this is <strong>very</strong> experimental and it is just a rule of thumb (you can see the documentation for a further explanation). Once the price of gasoline is calculated, any tolls are added and then a total cost is supplied. To do this, just change the parameter.</p>
<figure>
<pre><code data-lang="r">route2 &lt;- inegi_route(from = 6940, to = 7426, token = token, pref = 1, vehicle = 1, 
                      calc_cost = TRUE)
kable(route2$ROUTE)</code></pre>
</figure>
<table>
<thead>
<tr>
<th>KMS</th>
<th>TIME_MINS</th>
<th>TIME_HRS</th>
<th>HAS_TOLL</th>
<th>TOLL_COST</th>
<th>TOTAL_COST</th>
</tr>
</thead>
<tbody>
<tr>
<td>222.36</td>
<td>151.11</td>
<td>2.5185</td>
<td>TRUE</td>
<td>364</td>
<td>757.1729</td>
</tr>
</tbody>
</table>
<p>All prices are reported in Mexican pesos.</p>
<p>The second element in the list is the data.frame containing all point references in the route. As I said before, just connect the dots. Here is a preview:</p>
<figure>
<pre><code data-lang="r">kable(head(route$COORDINATE_PATH))</code></pre>
</figure>
<table>
<thead>
<tr>
<th>LONGITUD</th>
<th>LATITUD</th>
<th>INDEX</th>
</tr>
</thead>
<tbody>
<tr>
<td>-100.3125</td>
<td>25.66238</td>
<td>1</td>
</tr>
<tr>
<td>-100.3125</td>
<td>25.66231</td>
<td>2</td>
</tr>
<tr>
<td>-100.3124</td>
<td>25.66225</td>
<td>3</td>
</tr>
<tr>
<td>-100.3124</td>
<td>25.66222</td>
<td>4</td>
</tr>
<tr>
<td>-100.3124</td>
<td>25.66220</td>
<td>5</td>
</tr>
<tr>
<td>-100.3124</td>
<td>25.66215</td>
<td>6</td>
</tr>
</tbody>
</table>
<p>For this particular route, I added the dots in Google maps to show this better:</p>
<p><img src="https://i0.wp.com/www.enelmargen.org/images/posts/inegiR_2.jpg?w=456" data-recalc-dims="1"></p>
<h2>New GDP catalog</h2>
<p>Another huge issue that users reported was trying to find relevant indicator id’s in the INEGI webpage. As experienced users know, every economic data series has a unique id on the API. However, there is no catalog that allows you to find the id’s you are looking for. I have petitioned INEGI multiple times but got nowhere.</p>
<p>My personal solution was to look up the series in the BIE application (a web browser version of the API) and download the data as a .iqy object. From there, I would hack my way into the file to find the unique id’s being called. Very time intensive and error-prone.</p>
<p>So, to help each other out in this endeavour, I created a catalog of id’s. This version has all the sub-levels of GDP (up until 4th level desagregation), but I plan to update this catalog on a rolling basis. Any help would also be appreciated.</p>
<p>You can see the catalog by calling the dataset like this:</p>
<figure>
<pre><code data-lang="r">data(&quot;inegi_catalog&quot;)
kable(head(inegi_catalog[,1:7]))
# for more rows, see docs!</code></pre>
</figure>
<table>
<thead>
<tr>
<th>NAME</th>
<th>LEVEL_2</th>
<th>LEVEL_3</th>
<th>LEVEL_4</th>
<th>UNITS</th>
<th>BASE</th>
<th>FREQUENCY</th>
</tr>
</thead>
<tbody>
<tr>
<td>PIB</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
<tr>
<td>PIB – IMPUESTOS A PRODUCTOS NETOS</td>
<td>IMPUESTOS A PRODUCTOS NETOS</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
<tr>
<td>PIB – VALOR AGREGADO BRUTO</td>
<td>VALOR AGREGADO BRUTO</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
<tr>
<td>PIB – ACTIVIDADES PRIMARIAS</td>
<td>ACTIVIDADES PRIMARIAS</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
<tr>
<td>PIB – ACTIVIDADES PRIMARIAS – AGRICULTURA</td>
<td>ACTIVIDADES PRIMARIAS</td>
<td>AGRICULTURA</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
<tr>
<td>PIB – ACTIVIDADES SECUNDARIAS</td>
<td>ACTIVIDADES SECUNDARIAS</td>
<td>TOTAL</td>
<td>TOTAL</td>
<td>MILLIONS OF 2008 PESOS</td>
<td>2008</td>
<td>TRIMESTRAL</td>
</tr>
</tbody>
</table>
<h2>Compact metadata and series helper</h2>
<p>Two other common headaches came up with the past versions. First, the inegi_series() functions only accepted the full URL when most of the times, the only thing that changed between them was the number of the id. So I added a simple function to paste the entire URL string for the call to the API.</p>
<figure>
<pre><code data-lang="r">GPD_ID &lt;- 381016
inegi_code(381016)
# &quot;http://www3.inegi.org.mx/sistemas/api/indicadores/v1//Indicador/381016/00000/es/false/xml/&quot;</code></pre>
</figure>
<p>The second headache had to do with downloading multiple id’s. The list returned when using inegi_series() with the metadata parameter as TRUE is a bit clunky when using it in a loop or apply function. So I added a compact function that returns all the information in a tidy data.frame:</p>
<figure>
<pre><code data-lang="r">token_inegi &lt;- &quot;mytoken&quot;
df &lt;- compact_inegi_series(inegi_code(381016), token_inegi)
kable(head(df))</code></pre>
</figure>
<table>
<thead>
<tr>
<th>Values</th>
<th>Dates</th>
<th>Name</th>
<th>Update</th>
<th>Region</th>
<th>Units</th>
<th>Indicator</th>
<th>Frequency</th>
</tr>
</thead>
<tbody>
<tr>
<td>7945204</td>
<td>1993-01-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
<tr>
<td>7939362</td>
<td>1993-04-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
<tr>
<td>7954943</td>
<td>1993-07-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
<tr>
<td>8268036</td>
<td>1993-10-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
<tr>
<td>8210538</td>
<td>1994-01-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
<tr>
<td>8413362</td>
<td>1994-04-01</td>
<td>Producto interno bruto, a precios de mercado</td>
<td>2017/08/22</td>
<td>Nacional</td>
<td>Millones de pesos a precios de 2008</td>
<td>381016</td>
<td>Trimestral</td>
</tr>
</tbody>
</table>
<p>I hope this update is useful to everyone doing data science with Mexican stats. Any new suggestions or questiosn are welcome via <a rel="nofollow" href="https://twitter.com/eflores89" target="_blank">twitter</a> or a github <a rel="nofollow" href="https://github.com/Eflores89/inegiR" target="_blank">issue</a> request.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.enelmargen.org/datascience/inegiR-v2/"> En El Margen - R-English</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/_qo7bVs6hik" height="1"></a></div>