<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/how-to-learn-r-part-1-learn-from-a-master-data-scientists-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The R programming language is a powerful tool used in data science for business (DS4B), but R can be unnecessarily challenging to learn. We believe you can learn R quickly by taking an 80/20 approach to learning the most in-demand functions and packages. In this article, we seek to ultimately understand what techniques are most critical to a beginners success through analyzing a master data scientist’s code base. Half of this article covers the web scraping procedure (using <code>rvest</code> and <code>purrr</code>) we used to collect our data (if new to R, you can skip this). The second half covers the insights gained from analyzing a master’s code base. In the next article in our series, we’ll develop a strategic learning plan built on our knowledge of the master. Last, there’s a bonus at the end of the article that shows how you can analyze your own code base using the new <code>fs</code> package. Enjoy.</p>
<p><strong>Previous articles in the DS4B series</strong>:</p>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/business/2017/12/27/six-reasons-to-use-R-for-business.html" target="_blank">Six Reasons to Learn R for Business</a></li>
</ul>
<h2>Analyzing A Leader Through Their Code</h2>
<p>It’s no secret that the R Programming Community has a number of leaders. It’s one of the draws that separates R from the rest of the pack! The leaders have earned their leadership position by making an impact through high-end data science and unselfishly giving back to the community. We can all learn from what they do. The key is dissecting their code bases to understand the tools and techniques they use.</p>
<p>What we’ve done is made a first step in figuring out why these individuals are successful through analyzing their most frequently used tools. We started with one standout, a true master of data science…</p>
<h4>David Robinson (Our Master Data Scientist)</h4>
<p><img alt="David Robinson" src="https://i2.wp.com/www.business-science.io/assets/2018-03-03_how_to_learn_r/drob.png?w=456" data-recalc-dims="1"></p>
<p>We examined <a rel="nofollow" href="https://twitter.com/drob" target="_blank">David Robinson</a>, Chief Data Scientist at DataCamp (previously StackOverflow) (aka DRob), analyzing his code base by dissecting the functions and packages he uses regularly!</p>
<h4>Variance Explained Blog (Where We Got The Data)</h4>
<p>DRob actively maintains an excellent blog called <a rel="nofollow" href="http://varianceexplained.org/" target="_blank">Variance Explained</a>. He has 58 articles currently, most with code. We used the <code>rvest</code> package to collect the code contained in each post. We started with one blog post involving mixture models of baseball statistics. We then extended it to all 58 articles to increase our confidence in what tools he frequently uses.</p>
<h4>80/20 Analysis For Learning R (What We Did With The Data)</h4>
<p>We made several graphs that tell us interesting things about what R functions and packages DRob regularly uses. We then performed an 80/20 analysis on his code base. We output all of the high-usage functions and packages DRob regularly uses at the end of the article. This will be used in our next article developing a strategy to learn R efficiently.</p>
<p>Our first graph helps us answer which functions DRob routinely uses. Here are his top 20 functions.</p>
<p><img alt="Most Frequently Used Functions" src="https://i2.wp.com/www.business-science.io/assets/2018-03-03_how_to_learn_r/ve_functions_top_20.png?w=456" data-recalc-dims="1"></p>
<p>Our second graph helps us understand which packages are most frequently used based on the number of times functions within those packages appear in his code.</p>
<p><img alt="Most Frequently Used Packages" src="https://i2.wp.com/www.business-science.io/assets/2018-03-03_how_to_learn_r/ve_package_frequency_tbl.png?w=456" data-recalc-dims="1"></p>
<p>We noticed a theme that DRob is frequently using packages in the “tidyverse” (e.g. <code>dplyr</code>, <code>ggplot2</code>, etc). The third chart shows how “tidy” his code is by measuring the percentage of tidyverse functions vs non-tidyverse functions.</p>
<p><img alt="Tidiness Over Time" src="https://i1.wp.com/www.business-science.io/assets/2018-03-03_how_to_learn_r/ve_tidiness_over_time_tbl.png?w=456" data-recalc-dims="1"></p>
<p>And, finally, we listed out the top 88 functions in our 80/20 analysis. These are the functions he used 80% of the time. Check out the <a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#q4" target="_blank">80/20 list of most frequently used functions below</a>!</p>
<h4>Bonus: Analyze Your Code</h4>
<p>We show as a bonus how you can apply the custom scripts developed in this article to do an analysis on your code with the new <code>fs</code> package (short for file system). We briefly analyze our code base for the HR 201: Predicting Employee Attrition course that is under development for our <a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#bsu" target="_blank"><strong>Business Science University Virtual Workshop</strong></a> on predicting employee turnover and developing <code>Shiny</code> web applications. The Virtual Workshop is coming soon!</p>
<h2>Our Hypothesis</h2>
<p>Learning R can be unnecessarily challenging if one focuses on learning everything immediately rather than applying a strategic approach. Our hypothesis is composed of two theories:</p>
<ol>
<li>You do not need to learn everything to become effective (refer to <a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#80-20" target="_blank">80/20 Rule</a>)</li>
<li>You can learn a lot by analyzing others’ code bases (refer to <a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#master" target="_blank">Learning from a Master Data Scientist</a>)</li>
</ol>
<p>Our quest is to prove these theories.</p>
<h3>80/20 Rule <a></a></h3>
<p>Trying to solve every aspect of a challenge is overwhelming and often not the best use of your time. The 80/20 Rule can often help in these situations. Generally speaking, the 80/20 Rule posits that roughly 20% of activities or tasks produce 80% of the results you want. The challenge of learning the R language falls perfectly into this model. The key is figuring out which areas are the highest value for your time.</p>
<h3>Learning From A Master Data Scientist <a></a></h3>
<p>To make an optimal strategy, we need data on what tools are being used to perform high-end data science. But, where can we get data? From a data scientist that regularly performs high-end data science publicly via the internet. A true master at the dark art of data science!</p>
<p><strong>Why we chose David Robinson (aka DRob):</strong></p>
<p>I met DRob at the EARL Boston conference last November. He had just given a stellar presentation on StackOverflow trends concluding that R was growing rapidly. What stuck out about his presentation was his ability to analyze a question or problem – This is a key skill in DS4B!</p>
<p>Many data scientists such as top Kaggle competitors focus on how to create a high end predictions (which is important) but it’s not representative of most real-world situations. It’s really unique to see a data scientist effectively using problem solving and critical thinking in combination with data science to learn about the problem. DRob did this very well.</p>
<p>I had checked out his blog before, but never picked up on the fact that he’s really doing data science that can be applied to business (although he’s mainly applying to other areas such as sports and politics). DRob has written a number of articles on his blog, <a rel="nofollow" href="http://varianceexplained.org/" target="_blank">Variance Explained</a>. Some of my favorites are his articles on statistics applications in baseball. His approaches are novel leaning on Bayesian A/B testing, hierarchical modeling, mixture models, and many other tools that are very useful in business analysis. Further, he employs problem solving and critical thinking, which are the same skills that are needed in DS4B.</p>
<h2>Analysis: Learning From A Master</h2>
<p>If we treat DRob’s code on his Variance Explained blog as a text analysis, <strong>we can find the most frequently used packages and functions that cover the majority of code he produces</strong>. We can then use an 80/20 approach to determine which functions and packages are most used and therefore most important to master. We’ll split this analysis into two parts:</p>
<ul>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#part1" target="_blank">Part 1: Web Scraping The Variance Explained Blog</a> – Note this is a technical section showing how we retrieved the data. Novice learners may wish to skip this part.</p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#part2" target="_blank">Part 2: Learning From DRob’s Code</a> – The analytical work is done here!</p>
</li>
</ul>
<h3>Libraries</h3>
<p>If you wish to follow along, please load the following libraries.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tibbletime</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rvest</span><span>)</span><span>
</span><span>library</span><span>(</span><span>broom</span><span>)</span></code></pre>
</figure>
<h3>Part 1: Web Scraping the Variance Explained Blog <a></a></h3>
<p>CAUTION: Technical details ahead. Those new to R may wish to skip this section and <a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#part2" target="_blank">get right to the results</a>.</p>
<p>This part of the analysis is <strong>a how-to in web scraping</strong>. We expose the process to collect the data, which use the <code>rvest</code> package and a number of custom functions to parse the text. We split this part into two steps:</p>
<ul>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#parsing_functions" target="_blank">Step 1: Setup Code Parsing Functions</a></p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#web_scraping" target="_blank">Step 2: Web Scraping Variance Explained</a></p>
</li>
</ul>
<h4>Step 1: Setup Code Parsing and Utility Functions <a></a></h4>
<p>This is a text analysis. As such, we are going to need to parse some text to extract function names, to determine which packages functions belong to, and to analyze the text with counts and percents. To do so, we create a few helper functions:</p>
<ul>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#count_to_pct" target="_blank">count_to_pct()</a>: Utility function to quickly convert counts to percentages. Works well with <code>dplyr::count()</code>.</p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#parse_function_names" target="_blank">parse_function_names()</a>: Takes in text and returns function names.</p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#find_functions_in_package" target="_blank">find_functions_in_package()</a>: Takes in a library or package name and returns all functions in the library or package.</p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#find_loaded_packages" target="_blank">find_loaded_packages()</a>: Detects which packages are loaded in the R system.</p>
</li>
<li>
<p><a rel="nofollow" href="http://www.business-science.io/learning-r/2018/03/03/how_to_learn_R_pt1.html#map_loaded_package_functions" target="_blank">map_loaded_package_functions()</a>: Maps the function names to the respective package.</p>
</li>
</ul>
<h5>Count To Percent <a></a></h5>
<p><em>Function</em>: Utility function to quickly convert counts to percentages. Works well with <code>dplyr::count()</code>.</p>
<figure>
<pre><code data-lang="r"><span>count_to_pct</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>data</span><span>,</span><span> </span><span>...</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>n</span><span>)</span><span> </span><span>{</span><span>
</span><span>grouping_vars_expr</span><span> </span><span>&lt;-</span><span> </span><span>quos</span><span>(</span><span>...</span><span>)</span><span>
</span><span>col_expr</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>col</span><span>)</span><span>
</span><span>data</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>!!!</span><span> </span><span>grouping_vars_expr</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>pct</span><span> </span><span>=</span><span> </span><span>(</span><span>!!</span><span> </span><span>col_expr</span><span>)</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>!!</span><span> </span><span>col_expr</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span>
</span><span>}</span></code></pre>
</figure>
<p><em>Usage</em>: Use <code>dplyr::count()</code> to retrieve counts by grouping variables. Then use <code>count_to_pct()</code> to quickly get percentages within groups. Exclude groups if overall percentages are desired. Note this example uses the <code>mpg</code> data set from <code>ggplot2</code> package.</p>
<figure>
<pre><code data-lang="r"><span>mpg</span><span> </span><span>%>%</span><span>
</span><span>count</span><span>(</span><span>manufacturer</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>count_to_pct</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>top_n</span><span>(</span><span>5</span><span>)</span><span> </span><span>%>%</span><span> </span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>n</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 5 x 3
##   manufacturer     n    pct
##   </code></pre></figure>