<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/american-vs-national-league-woba-vales/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.datascienceriot.com//r/lgwoba/"> Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Traditionally, statistics like wOBA (weighted on-base average) have been calculated using league averages. While building the <code>baseballDBR</code> package, I thought it would be interesting to group the American and National when making wOBA calculations. In theory, there should be parity across the two leagues, but that is not always the case.</p>
<p>In order to calculate wOBA values for each league, the <code>baseballDBR</code> package uses a ported version of Tom Tango’s <a rel="nofollow" href="http://www.insidethebook.com/ee/index.php/site/article/woba_year_by_year_calculations/" target="_blank">SQL incantation</a> to calculate wOBA using the Baseball Databank. While Tango admits, this calculation is not perfect, it normally has a plus/minus of less than one one-thousandth of a percent compared to <a rel="nofollow" href="http://www.fangraphs.com/guts.aspx?type=cn" target="_blank">Fangraphs’ values</a>.</p>
<h2>Gathering wOBA Modifiers by League</h2>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;baseballDBR&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>baseballDBR</span><span>)</span><span>
</span><span># Load data from Baseball Databank
</span><span>get_bbdb</span><span>(</span><span>table</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Batting&quot;</span><span>,</span><span> </span><span>&quot;Pitching&quot;</span><span>,</span><span> </span><span>&quot;Fielding&quot;</span><span>))</span><span>
</span><span># Run wOBA values for seperate leagues.
# Subset for 1900, when the AL was created. Exclude the Federal League.
</span><span>w_vals</span><span> </span><span>&lt;-</span><span> </span><span>wOBA_values</span><span>(</span><span>BattingTable</span><span> </span><span>=</span><span> </span><span>Batting</span><span>,</span><span> </span><span>FieldingTable</span><span> </span><span>=</span><span> </span><span>Fielding</span><span>,</span><span> </span><span>PitchingTable</span><span> </span><span>=</span><span> </span><span>Pitching</span><span>,</span><span> </span><span>Sep.Leagues</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>subset</span><span>(</span><span>yearID</span><span> </span><span>>=</span><span> </span><span>1900</span><span> </span><span>&amp;</span><span> </span><span>lgID</span><span> </span><span>!=</span><span> </span><span>&quot;FL&quot;</span><span>,</span><span> </span><span>select</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;yearID&quot;</span><span>,</span><span> </span><span>&quot;lgID&quot;</span><span>,</span><span> </span><span>&quot;lg_woba&quot;</span><span>))</span><span>
</span><span>head</span><span>(</span><span>w_vals</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 6 x 3
##   yearID  lgID   lg_woba
##    </code></pre></figure>