<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/new-banxicor-package/"></div></div><div><a data-url="https://www.r-bloggers.com/new-banxicor-package/" data-counturl="https://www.r-bloggers.com/new-banxicor-package/" data-count="horizontal" data-text="New banxicoR package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.enelmargen.org/banxicoR/vignette-v09"> 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>The <strong>banxicoR</strong> package is now available on CRAN. Much like <a target="_blank" href="https://github.com/Eflores89/inegiR">inegiR</a> this package aims to bring official Mexican data easily into R, in this case by scrapping <em>iqy</em> calls to the SIE (Sistema de Información Económica) webservice of the Bank of Mexico.</p>
<h3>Introduction</h3>
<p>The major difference with inegiR is that the Bank of Mexico does not have an API, so this package basically uses <code>rvest</code> to scrape the generated html. The package then does what it can to save it in a convenient <code>data.frame</code> or <code>list</code> (same as inegiR).</p>
<p>A few caveats:</p>
<ul>
<li>I do not control the data definitions at the source (Banxico), so I can’t guarantee continuos use.</li>
<li>Finding the ID of each series has proven a manual, slow and tedious process because they are not available in the webpage. I’m trying to contact Banxico about getting a catalog, but basically use at your own risk.</li>
</ul>
<h3>Finding series ID’s</h3>
<p>To find a specific series ID, I would recommend going to the <a target="_blank" href="http://www.banxico.org.mx/SieInternet/">SIE webpage</a>, navigating towards the desired indicators and then consulting them via HTML. The column name should be the series id (they are usually in this format: “SF60653”, with two characters followed by numbers). The package includes a <strong>small and non exhaustive</strong> catalog of series <strong>in spanish</strong>. You can access this by <code>data(&quot;BanxicoCatalog&quot;)</code>.</p>
<p>Then you can find some id’s…</p>
<div>
<pre><code data-lang="r"><span>library</span><span>(</span>banxicoR<span>)</span>
<span>library</span><span>(</span>dplyr<span>)</span>
data<span>(</span><span>&quot;BanxicoCatalog&quot;</span><span>)</span>

<span># To see how many id's by parent subject </span>
BanxicoCatalog <span>%>%</span> 
  group_by<span>(</span>PARENT<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;Id's&quot;</span> <span>=</span> n<span>())</span>

<span># Source: local data frame [4 x 2]</span>

<span># PARENT Id's</span>
<span># (chr) (int)</span>
<span># 1 Billetes y Monedas 45</span>
<span># 2 Intermediarios Financierios 37</span>
<span># 3 Sistemas de Pago 49</span>
<span># 4 Tipo de Cambio 2</span>

<span># to bring the specific id of the average duration of 200 peso bills...</span>
BanxicoCatalog <span>%>%</span> 
  filter<span>(</span>PARENT <span>==</span> <span>&quot;Billetes y Monedas&quot;</span><span>)</span> <span>%>%</span> 
  filter<span>(</span>LEVEL_1 <span>==</span> <span>&quot;Duración promedio del billete&quot;</span><span>)</span> <span>%>%</span> 
  filter<span>(</span>LEVEL_2 <span>==</span> <span>&quot;200 pesos&quot;</span><span>)</span> <span>%>%</span> 
  select<span>(</span>ID<span>)</span>

<span># Source: local data frame [1 x 1]</span>

<span># ID</span>
<span># (chr)</span>
<span>#1 SM32</span></code></pre>
</div>
<h3>Usage</h3>
<p>Now that you have some id’s to download, we can use the <code>banxico_series</code> function…</p>
<div>
<pre><code data-lang="r"><span># Download the Bank of Mexico international reserves</span>
rsv <span>&lt;-</span> banxico_series<span>(</span>series <span>=</span> <span>&quot;SF110168&quot;</span><span>)</span>

<span>tail</span><span>(</span>rsv<span>)</span>
<span># DATE SF110168</span>
<span># 2016-01-01 176321.4</span>
<span># 2016-02-01 178408.8</span>
<span># 2016-03-01 179708.0</span>
<span># 2016-04-01 182118.8</span>
<span># 2016-05-01 179351.0</span>
<span># 2016-06-01 178829.9</span></code></pre>
</div>
<p>If you want some other fancy things, you can use the options…</p>
<div>
<pre><code data-lang="r">rsv <span>&lt;-</span> banxico_series<span>(</span>series <span>=</span> <span>&quot;SF110168&quot;</span><span>,</span> 
                      metadata <span>=</span> <span>TRUE</span><span>,</span> 
                      verbose <span>=</span> <span>TRUE</span><span>)</span>
<span># [1] &quot;Data series: SF110168 downloaded&quot;</span>
<span># [1] &quot;Data series in monthly frequency&quot;</span>
<span># [1] &quot;Parsing data with 198 rows&quot;</span>

str<span>(</span>rsv<span>)</span>
<span>#List of 2</span>
<span># $ MetaData:List of 6</span>
<span># ..$ IndicatorName: chr &quot;I. Official Reserve Assets And Other Foreign Currency Assets - A. Official Reserve Assets&quot;</span>
<span># ..$ IndicatorId : chr &quot;SF110168&quot;</span>
<span># ..$ Units : chr &quot;millions of u.s. dollar&quot;</span>
<span># ..$ DataType : chr &quot;market value/price&quot;</span>
<span># ..$ Period : chr &quot;jan 2000 - jun 2016&quot;</span>
<span># ..$ Frequency : chr &quot;monthly&quot;</span>
<span># $ Data :'data.frame':	198 obs. of 2 variables:</span>
<span># ..$ Dates : Date[1:198], format: &quot;2000-01-01&quot; ...</span>
<span># ..$ SF110168: num [1:198] 33689 33382 36435 34749 33624 ...</span></code></pre>
</div>
<p>Finally, we can graph this…</p>
<div>
<pre><code data-lang="r"><span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>eem<span>)</span> <span># theme from: https://github.com/Eflores89/eem</span>

ggplot<span>(</span>rsv<span>$</span>Data<span>,</span> 
       aes<span>(</span>x <span>=</span> Dates<span>,</span> y <span>=</span> SF110168<span>))</span><span>+</span>
       geom_path<span>(</span>colour <span>=</span> eem_colors<span>[</span><span>1</span><span>])</span><span>+</span>
       theme_eem<span>()</span><span>+</span>
       labs<span>(</span>x <span>=</span> <span>&quot;Dates&quot;</span><span>,</span> 
            y <span>=</span> <span>paste0</span><span>(</span><span>&quot;Reserves in U.S. Dollars \n (&quot;</span><span>,</span> rsv<span>$</span>MetaData<span>$</span>Units<span>,</span> <span>&quot;)&quot;</span><span>),</span> 
            title <span>=</span> <span>&quot;Bank of Mexico International Reserves&quot;</span><span>)</span></code></pre>
</div>
<p><img alt="bank of mexico reserves" src="https://i2.wp.com/enelmargen.org/banxicoR/imgs/banxicoR_1.png?w=456" data-recalc-dims="1"></p>
<p>This data series is also available at INEGI and can be downloaded with <a target="_blank" href="https://github.com/Eflores89/inegiR">inegiR</a> but Banxico has other interesting series exclusive to them, like financial loans or money in circulation, which I encourage everyone to check out!</p>
<p><a target="_blank" href="https://twitter.com/eflores89">Tweet</a> me up if you have any suggestions / improvements or open <a target="_blank" href="https://github.com/Eflores89/banxicoR">an issue at Github</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/new-banxicor-package/"></div></div><div><a data-url="https://www.r-bloggers.com/new-banxicor-package/" data-counturl="https://www.r-bloggers.com/new-banxicor-package/" data-count="vertical" data-text="New banxicoR package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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/banxicoR/vignette-v09"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oUsnFACE_to"></a></div>