<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/calculate-wages-and-benefits-in-r-with-blscraper/"></div></div><div><a data-url="https://www.r-bloggers.com/calculate-wages-and-benefits-in-r-with-blscraper/" data-counturl="https://www.r-bloggers.com/calculate-wages-and-benefits-in-r-with-blscraper/" data-count="horizontal" data-text="Calculate Wages and Benefits in R with blscrapeR" 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="https://www.datascienceriot.com/calculate-wages-and-benefits-in-r-with-blscraper/kris/"> R Tricks – Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The most difficult thing about working with BLS data is gaining a clear understanding on what data are available and what they represent. Some of the more popular data sets can be found on the <a target="_blank" href="http://www.bls.gov/data/">BLS Databases, Tables &amp; Calculations</a> website. The selected examples below do not include all series or databases.</p>
<p><strong>Install blscrapeR</strong><br>
The first step in analyzing any of these data in R is to install the blscrapeR package from CRAN.</p>
<pre>
install.packages('blscrapeR')
</pre>
<p><strong>Current Population Survey (CPS)</strong><br>
The <a target="_blank" href="http://www.bls.gov/cps/data.htm">CPS</a> includes median weekly earnings by occupation, among other things.</p>
<p>For example, we can use blscrapeR to pull data from the API for the median weekly earnings for Database Administrators and Software Developers.</p>
<pre>
library(blscrapeR)
# Median Usual Weekly Earnings by Occupation, Unadjusted Second Quartile.
# In current dollars
df &lt;- bls_api(c(&quot;LEU0254530800&quot;, &quot;LEU0254530600&quot;),
                startyear = 2000, endyear = 2015)
# Plot
library(ggplot2)
ggplot(df, aes(x=date, y=value, color=seriesID)) +
    geom_line() +
    labs(title = &quot;Median Weekly Earnings by Occupation&quot;) +
    theme(legend.position=&quot;top&quot;) +
    scale_color_discrete(name=&quot;Occupation&quot;,
        breaks=c(&quot;LEU0254530800&quot;, &quot;LEU0254530600&quot;),
        labels=c(&quot;Database Admins.&quot;, &quot;Software Devs.&quot;))
</pre>
<p><a target="_blank" href="https://i0.wp.com/www.datascienceriot.com/wp-content/uploads/2016/08/WeeklyEarningsPlotBLS.png?ssl=1"><img sizes="(max-width: 544px) 100vw, 544px" alt="WeeklyEarningsPlotBLS" class="wp-image-960" src="https://i0.wp.com/www.datascienceriot.com/wp-content/uploads/2016/08/WeeklyEarningsPlotBLS.png?resize=456%2C298&ssl=1" data-recalc-dims="1"></a></p>
<p><strong>Occupational Employment Statistics (OES)</strong></p>
<p>The <a target="_blank" href="http://beta.bls.gov/dataQuery/find?fq=survey:%5Boe%5D&q=oe%22%20target=%22_blank">OES</a> contains similar wage data found in the CPS, but often has more resolution in certain geographic areas. Unlike the CPS, the OES is an annual survey and does not keep time series data.</p>
<p>For example, we may want to compare the average hourly wage of Computer and Information Systems Managers in Orlando, FL to those in San Jose, CA. Notice, below the survey only returns values for 2015.</p>
<pre>
# Computer and Information Systems Managers in Orlando, FL and San Jose, CA.
# Orlando: &quot;OEUM003674000000011302103&quot;
# San Jose: &quot;OEUM004194000000011302108&quot;
library(blscrapeR)
df &lt;- bls_api(c(&quot;OEUM003674000000011302103&quot;, &quot;OEUM004194000000011302108&quot;))
head(df)
</pre>
<p>Output:</p>
<pre>
##   year period periodName value footnotes                  seriesID         date
## 1 2015    A01     Annual 68.03           OEUM003674000000011302103   2015-01-31
## 2 2015    A01     Annual 85.86           OEUM004194000000011302108   2015-01-31
</pre>
<p>Another OES example would be to grab the most recent Annual mean wage for All Occupations in All Industries in the United States.</p>
<pre>
library(blscrapeR)
df &lt;- bls_api(&quot;OEUN000000000000000000004&quot;)
df
</pre>
<p>Output:</p>
<pre>
##   year period periodName value footnotes                  seriesID        date
## 1 2015    A01     Annual 48320           OEUN000000000000000000004   2015-01-31
</pre>
<p><strong>Employer Cost for Employee Compensation</strong></p>
<p>This<a target="_blank" href="http://www.bls.gov/news.release/ecec.toc.htm"> data set</a> includes time series data on how much employers pay for employee benefits as a total cost and as a percent of employee wages and salaries.</p>
<p>For example, if we want to see the total cost of benefits per hour work and also see what percentage that is of the total compensation, we could run the following script.</p>
<pre>
library(blscrapeR)
library(dplyr)
library(tidyr)
df &lt;- bls_api(c(&quot;CMU1030000000000D&quot;, &quot;CMU1030000000000P&quot;))

# Spread series ids and rename columns to human readable format.
df.sp &lt;- spread(df, seriesID, value) %>%
    rename(&quot;hourly_cost&quot;=CMU1030000000000D, &quot;pct_of_wages&quot;=CMU1030000000000P)

# Percentages are represented as floating integers. Fix this to avoid confusion.
df.sp$pct_of_wages &lt;- df.sp$pct_of_wages*0.01
df.sp
</pre>
<p>Output:</p>
<pre>
     year period  periodName footnotes       date hourly_cost pct_of_wages
## 1 2014    Q01 1st Quarter           2014-01-31        9.97        0.312
## 2 2014    Q02 2nd Quarter           2014-02-28       10.00        0.313
## 3 2014    Q03 3rd Quarter           2014-03-31       10.07        0.313
## 4 2014    Q04 4th Quarter           2014-04-30       10.49        0.316
## 5 2015    Q01 1st Quarter           2015-01-31       10.61        0.317
## 6 2015    Q02 2nd Quarter           2015-02-28       10.47        0.315
## 7 2015    Q03 3rd Quarter           2015-03-31       10.48        0.314
## 8 2015    Q04 4th Quarter           2015-04-30       10.52        0.313
## 9 2016    Q01 1st Quarter           2016-01-31       10.70        0.315
</pre>
<p><strong>National Compensation Survey-Benefits</strong></p>
<p><a target="_blank" href="http://www.bls.gov/ncs/ebs/">This survey</a> includes data on how many Americans have access to certain benefits. For example, we can see the percentage of those who have access to paid vacation days and those who have access to Health insurance through their employers.</p>
<pre>
library(blscrapeR)
library(dplyr)
library(tidyr)
df &lt;- bls_api(c(&quot;NBU10500000000000033030&quot;, &quot;NBU11500000000000028178&quot;))

# Spread series ids and rename columns to human readable format.
df.sp &lt;- spread(df, seriesID, value) %>%
    rename(&quot;pct_paid_vacation&quot;=NBU10500000000000033030, &quot;pct_health_ins&quot;=NBU11500000000000028178)

# Value data are in whole numbers but represent percentages. Fix this to avoid confusion.
df.sp$pct_paid_vacation &lt;- df.sp$pct_paid_vacation*0.01
df.sp$pct_health_ins &lt;- df.sp$pct_health_ins*0.01
df.sp
</pre>
<p>Output:</p>
<pre>
     year period periodName footnotes       date pct_paid_vacation   pct_health_ins
## 1 2013    A01     Annual           2013-01-31              0.74             0.72
## 2 2014    A01     Annual           2014-01-31              0.74             0.72
## 3 2015    A01     Annual           2015-01-31              0.74             0.72
</pre>
<p>For more on blscrapeR, you can visit the package's <a target="_blank" href="https://github.com/keberwein/blscrapeR/tree/master/vignettes">vignettes directory</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/calculate-wages-and-benefits-in-r-with-blscraper/"></div></div><div><a data-url="https://www.r-bloggers.com/calculate-wages-and-benefits-in-r-with-blscraper/" data-counturl="https://www.r-bloggers.com/calculate-wages-and-benefits-in-r-with-blscraper/" data-count="vertical" data-text="Calculate Wages and Benefits in R with blscrapeR" 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="https://www.datascienceriot.com/calculate-wages-and-benefits-in-r-with-blscraper/kris/"> R Tricks – 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 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/p-5n3aZhZdI"></a></div>