<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/parallel-processing-baseball-data-with-r-and-mlbgameday/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.datascienceriot.com//r/mlbgameday-basics/"> Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Just In Time For Baseball</h2>
<p>The <code>mlbgameday</code> package has just reached the milestone of version 0.1.0.</p>
<p>Designed to facilitate extract, transform and load for MLBAM “Gameday” data. The package is optimized for parallel processing of data that may be larger than memory. There are other packages in the R universe that were built to perform statistics and visualizations on these data, but <a rel="nofollow" href="https://github.com/keberwein/mlbgameday" target="_blank">mlbgameday</a> is concerned primarily with data collection. More uses of these data can be found in the <a rel="nofollow" href="https://github.com/cpsievert/pitchRx" target="_blank">pitchRx</a>, <a rel="nofollow" href="https://github.com/beanumber/openWAR" target="_blank">openWAR</a>, and <a rel="nofollow" href="https://github.com/BillPetti/baseballr" target="_blank">baseballr</a> packages.</p>
<h2>Install from CRAN</h2>
<p><code>install.packages(&quot;mlbgameday&quot;)</code></p>
<h2>Parallel Processing</h2>
<p>The package’s internal functions are optimized to work with the <code>doParallel</code> package. By default, the R language will use one core of our CPU. The <code>doParallel</code> package enables us to use several cores, which will execute tasks simultaneously. In a standard regular season for all teams, the function has to process more than 2,400 individual files, which depending on your system, can take quite some time. Parallel processing speeds this process up by several times, depending on how many processor cores we choose to use.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>mlbgameday</span><span>)</span><span>
</span><span>library</span><span>(</span><span>doParallel</span><span>)</span><span>
</span><span># First we need to register our parallel cluster.</span><span>
</span><span># Set the number of cores to use as the machine's maximum number of cores minus 1 for background processes.</span><span>
</span><span>no_cores</span><span> </span><span>&lt;-</span><span> </span><span>detectCores</span><span>()</span><span> </span><span>-</span><span> </span><span>1</span><span>
</span><span>cl</span><span> </span><span>&lt;-</span><span> </span><span>makeCluster</span><span>(</span><span>no_cores</span><span>)</span><span> </span><span>registerDoParallel</span><span>(</span><span>cl</span><span>)</span><span>
</span><span># Then run the get_payload function as normal.</span><span>
</span><span>innings_df</span><span> </span><span>&lt;-</span><span> </span><span>get_payload</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>&quot;2017-04-03&quot;</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>&quot;2017-04-10&quot;</span><span>)</span><span>
</span><span># Don't forget to stop the cluster when finished.</span><span>
</span><span>stopImplicitCluster</span><span>()</span><span>
</span><span>rm</span><span>(</span><span>cl</span><span>)</span></code></pre>
</figure>
<h2>Non Parallel</h2>
<p>Although the package is optimized for parallel processing, it will also work without registering a parallel backend. When only querying a single day’s data, a parallel backend may not provide much additional performance. However, parallel backends are suggested for larger data sets, as the process will be faster by several orders of magnitude.</p>
<p>We can download and subset a small amount of data. In the example below, we’ll look for Jake Arrienta’s no-hitter in 2016.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>mlbgameday</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span># Grap some Gameday data. We're specifically looking for Jake Arrieta's no-hitter.</span><span>
</span><span>gamedat</span><span> </span><span>&lt;-</span><span> </span><span>get_payload</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>&quot;2016-04-21&quot;</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>&quot;2016-04-21&quot;</span><span>)</span><span>
</span><span># Subset that atbat table to only Arrieta's pitches and join it with the pitch table.</span><span>
</span><span>pitches</span><span> </span><span>&lt;-</span><span> </span><span>inner_join</span><span>(</span><span>gamedat</span><span>$</span><span>pitch</span><span>,</span><span> </span><span>gamedat</span><span>$</span><span>atbat</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;num&quot;</span><span>,</span><span> </span><span>&quot;url&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>subset</span><span>(</span><span>pitcher_name</span><span> </span><span>==</span><span> </span><span>&quot;Jake Arrieta&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/keberwein/mlbgameday/master/man/figures/mlbgameday_hex.png?w=456&ssl=1" data-recalc-dims="1"></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.datascienceriot.com//r/mlbgameday-basics/"> Data Science Riot!</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/00gSRvIUqIQ" height="1"></a></div>