<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/my-advice-on-dplyrmutate/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2017/09/my-advice-on-dplyrmutate/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There are substantial differences between ad-hoc analyses (be they: machine learning research, data science contests, or other demonstrations) and production worthy systems. Roughly: ad-hoc analyses have to be correct only at the moment they are run (and often once they are correct, that is the last time they are run; obviously the idea of reproducible research is an attempt to raise this standard). Production systems have to be durable: they have to remain correct as models, data, packages, users, and environments change over time.</p>
<p>Demonstration systems need merely glow in bright light among friends; production systems <em>must</em> be correct, even alone in the dark.</p>
<p><center><br>
<img border="0" alt="Vlcsnap 00887" title="vlcsnap-00887.jpg" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/09/vlcsnap-00887.jpg?resize=417%2C178" data-recalc-dims="1">
<p>
“Character is what you are in the dark.”
<p>
<a rel="nofollow" href="http://www.imdb.com/title/tt0086856/" target="_blank">John Whorfin</a> quoting <a rel="nofollow" href="https://en.wikiquote.org/wiki/Dwight_L._Moody" target="_blank">Dwight L. Moody</a>.<br>
</center></p>
<p>I have found: to deliver production worthy data science and predictive analytic systems, one has to develop per-team and per-project field tested recommendations and best practices. This is necessary even when, or <em>especially</em> when, these procedures differ from official doctrine. </p>
<p>What I want to do is share a single small piece of <a rel="nofollow" href="http://www.win-vector.com/site/" target="_blank">Win-Vector LLC</a>‘s current guidance on using the <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> package <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank"><code>dplyr</code></a>.<span></span></p>
<p>
<ul>
<li>
<strong>Disclaimer:</strong> Win-Vector LLC has no official standing with RStudio, or <code>dplyr</code> development. </li>
<li>
<p><strong>However:</strong></p>
<blockquote><p>
“One need not have been Caesar in order to understand Caesar.”</p>
<p>Alternately: Georg Simmmel or Max Webber.

</blockquote>
<p>Win-Vector LLC, as a consultancy, has experience helping large companies deploy enterprise big data solutions involving <code>R</code>, <code>dplyr</code>, <code>sparklyr</code>, and <code>Apache Spark</code>. Win-Vector LLC, as a training organization, has experience in how new users perceive, reason about, and internalize how to use <code>R</code> and <code>dplyr</code>. Our group knows how to help deploy production grade systems, and how to help new users master these systems.


<p>From experience we have distilled a lot of best practices. And below we will share one.</p>
<p>From: “<a rel="nofollow" href="http://r4ds.had.co.nz/transform.html#add-new-variables-with-mutate" target="_blank"><em>R for Data Science</em>; Whickham, Grolemund; O’Reilly, 2017</a>” we have:</p>
<blockquote><p>
Note that you can refer to columns that you’ve just created:</p>
<pre>
mutate(flights_sml,
   gain = arr_delay - dep_delay,
   hours = air_time / 60,
   gain_per_hour = gain / hours
 )
</pre>
</blockquote>
<p>Let’s try that with database backed data:</p>
<pre>
suppressPackageStartupMessages(library(&quot;dplyr&quot;))
packageVersion(&quot;dplyr&quot;)
# [1] ‘0.7.3’

db &lt;- DBI::dbConnect(RSQLite::SQLite(), 
                     &quot;:memory:&quot;)
flights &lt;- copy_to(db, 
             nycflights13::flights,
             'flights')

mutate(flights,
       gain = arr_delay - dep_delay,
       hours = air_time / 60,
       gain_per_hour = gain / hours
)
# # Source:   lazy query [?? x 22]
# # Database: sqlite 3.19.3 [:memory:]
# year month   day dep_time sched_dep_time        ...
# </pre></ul>