<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/comparative-examples-using-replyrlet/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/12/comparative-examples-using-replyrlet/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Consider the problem of “parametric programming” in <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">R</a>. That is: simply writing correct code before knowing some details, such as the names of the columns your procedure will have to be applied to in the future. Our latest version of <a target="_blank" rel="nofollow" href="https://github.com/WinVector/replyr"><code>replyr::let</code></a> makes such programming easier. </p>
<p><center><br>
<a target="_blank" rel="nofollow" href="http://www.misterkitty.org/extras/stupidcovers/stupidcomics507.html"><img title="NewImage.png" alt="NewImage" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/12/NewImage-8.png?resize=323%2C470" border="0" data-recalc-dims="1"><br>
Archie’s Mechanics #2 (1954) copyright Archie Publications</a>
<p>(edit: great news! CRAN just accepted our <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=replyr"><code>replyr 0.2.0</code> fix release</a>!) </p>
<p>Please read on for examples comparing standard notations and <code>replyr::let</code>.<span></span></p>
<p>Suppose, for example, your task was to and build a new advisory column that tells you which values in a column of a <code>data.frame</code> are missing or <code>NA</code>. We will illustrate this in <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">R</a> using the example data given below:</p>
<div>
<pre><code>d &lt;-<span> </span><span>data.frame</span>(<span>x =</span> <span>c</span>(<span>1</span>, <span>NA</span>))
<span>print</span>(d)
 <span># x</span>
 <span># 1 1</span>
 <span># 2 NA</span></code></pre>
</div>
<p>Performing an ad hoc analysis is trivial in <code>R</code>: we would just directly write:</p>
<div>
<pre><code>d$x_isNA &lt;-<span> </span><span>is.na</span>(d$x)</code></pre>
</div>
<p>We used the fact that we are looking at the data interactively to note the only column is “<code>x</code>”, and then picked “<code>x_isNA</code>” as our result name. If we want to use <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr"><code>dplyr</code></a> the notation remains straightforward:</p>
<div>
<pre><code><span>library</span>(<span>&quot;dplyr&quot;</span>)
 <span># </span>
 <span># Attaching package: 'dplyr'</span>
 <span># The following objects are masked from 'package:stats':</span>
 <span># </span>
 <span># filter, lag</span>
 <span># The following objects are masked from 'package:base':</span>
 <span># </span>
 <span># intersect, setdiff, setequal, union</span>
d %>%<span> </span><span>mutate</span>(<span>x_isNA =</span> <span>is.na</span>(x))
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
<p>Now suppose, as is common in actual data science and data wrangling work, we are not the ones picking the column names. Instead suppose we are trying to produce reusable code to perform this task again and again on many data sets. In that case we would then expect the column names to be given to us as values inside other variables (i.e., as parameters).</p>
<div>
<pre><code>cname &lt;-<span> &quot;x&quot;</span>                            <span># column we are examining</span>
rname &lt;-<span> </span><span>paste</span>(cname, <span>&quot;isNA&quot;</span>, <span>sep=</span> <span>'_'</span>) <span># where to land results</span>
<span>print</span>(rname)
 <span># [1] &quot;x_isNA&quot;</span></code></pre>
</div>
<p>And writing the matching code is again trivial:</p>
<div>
<pre><code>d[[rname]] &lt;-<span> </span><span>is.na</span>(d[[cname]])</code></pre>
</div>
<p>We are now programming at a slightly higher level, or automating tasks. We don’t need to type in new code each time a new data set with a different column name comes in. It is now easy to write a <code>for-loop</code> or <code>lapply</code> over a list of columns to analyze many columns in a single data set. It is an absolute travesty when something that is purely virtual (such as formulas and data) can not be automated over. So the slightly clunkier “<code>[[]]</code>” notation (which can be automated) is a necessary complement to the more convenient “<code>$</code>” notation (which is too specific to be easily automated over).</p>
<p>Using <code>dplyr</code> directly (when you know all the names) is deliberately straightforward, but programming over <code>dplyr</code> can become a challenge.</p>
<div>
<h2>Standard practice</h2>
<p>The standard parametric <code>dplyr</code> practice is to use <code>dplyr::mutate_</code> (the standard evaluation or parametric variation of <code>dplyr::mutate</code>). Unfortunately the notation in using such an “underbar form” is currently cumbersome.</p>
<p>You have the choice building up your formula through variations of one of:</p>
<ul>
<li>A formula</li>
<li>Using <code>quote()</code></li>
<li>A string</li>
</ul>
<p>(source: <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html">dplyr Non-standard evaluation</a>, for additional theory and upcoming official solutions please see <a target="_blank" rel="nofollow" href="https://rpubs.com/hadley/lazyeval">here</a>).</p>
<p>Let us try a few of these to try and emphasize we are proposing a new solution, not because we do not know of the current solutions, but instead because we are familiar with the current solutions.</p>
<div>
<h3>Formula interface</h3>
<p>Formula interface is a nice option as it is <code>R</code>’s common way for holding names unevaluated. The code looks like the following:</p>
<div>
<pre><code>d %>%<span> </span><span>mutate_</span>(<span>RCOL =</span> lazyeval::<span>interp</span>(~<span> </span><span>is.na</span>(cname))) %>%
<span> </span><span>rename_</span>(<span>.dots =</span> stats::<span>setNames</span>(<span>'RCOL'</span>, rname))
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA FALSE</span></code></pre>
</div>
<p>Currently <code>mutate_</code> does not take “two-sided formulas” so we need to control names outside of the formula. In this case we used the explicit <code>dplyr::rename_</code> because attempting to name the assignment in-line does not seem to be supported (or if it is supported, it uses a different notation or convention than the one we have just seen):</p>
<div>
<pre><code><span># the following does not correctly name the result column</span>
d %>%<span> </span><span>mutate_</span>(<span>.dots =</span> stats::<span>setNames</span>(lazyeval::<span>interp</span>( ~<span> </span><span>is.na</span>(cname)),
                                      rname))
 <span># x is.na(cname)</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA FALSE</span></code></pre>
</div>
</div>
<div>
<h3>Trying <code>quote()</code></h3>
<p><code>quote()</code> can delay evaluation, but isn’t the right tool for parameterizing (what the linked NSE reference called “mixing constants and variable”). We can only conveniently get about halfway to the solution (output parameterized, input hard-coded as “x”).</p>
<div>
<pre><code><span># dplyr mutate_ paste stats::setNames solution</span>
d %>%<span> </span><span>mutate_</span>(<span>.dots =</span>
                stats::<span>setNames</span>(<span>quote</span>(<span>is.na</span>(x)),
                rname))
 <span># x is.na(x)</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
<p>My point is: even if this is something that <em>you</em> know how to accomplish, this is evidence we are really trying to swim upstream with this notation.</p>
</div>
<div>
<h3>String solutions</h3>
<p>String based solutions can involve using <code>paste</code> to get parameter values into the strings. Here is an example:</p>
<div>
<pre><code><span># dplyr mutate_ paste stats::setNames solution</span>
d %>%<span> </span><span>mutate_</span>(<span>.dots =</span>
                stats::<span>setNames</span>(<span>paste0</span>(<span>'is.na('</span>, cname, <span>')'</span>),
                rname))
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
<p>Or just using strings as an interface to control <code>lazyeval::interp</code>:</p>
<div>
<pre><code><span># dplyr mutate_ lazyeval::interp solution</span>
d %>%<span> </span><span>mutate_</span>(<span>RCOL =</span>
                lazyeval::<span>interp</span>(<span>&quot;is.na(cname)&quot;</span>,
                <span>cname =</span> <span>as.name</span>(cname))) %>%
<span> </span><span>rename_</span>(<span>.dots =</span> <span>setNames</span>(<span>'RCOL'</span>, rname))
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
</div>
</div>
<div>
<h2>Our advice</h2>
<p>Our advice is to give <a target="_blank" rel="nofollow" href="https://github.com/WinVector/replyr"><code>replyr::let</code></a> a try. <code>replyr::let</code> takes a name mapping list (called “<code>alias</code>”) and a code-block (called “<code>expr</code>”). The code-block is re-written so that names in <code>expr</code> appearing on the left hand sides of the <code>alias</code> map are replaced with names appearing on the right hand side of the <code>alias</code> map.</p>
<p>The code looks like this:</p>
<div>
<pre><code><span># replyr::let solution</span>
replyr::<span>let</span>(<span>alias =</span> <span>list</span>(<span>cname =</span> cname, <span>rname =</span> rname),
            <span>expr =</span> {
            d %>%<span> </span><span>mutate</span>(<span>rname =</span> <span>is.na</span>(cname))
            })
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
<p>Notice we are able to use <code>dplyr::mutate</code> instead of needing to invoke <code>dplyr::mutate_</code>. The expression block can be arbitrarily long and contain deep pipelines. We now have a useful separation of concerns, the mapping code is a wrapper completely outside of the user pipeline (the two are no longer commingled). For complicated tasks the ratio of <code>replyr::let</code> boilerplate to actual useful work goes down quickly.</p>
<p>We also have a varation for piping into (though to save such pipes for later you use <code>replyr::let</code>, not <code>replyr::letp</code>):</p>
<div>
<pre><code><span># replyr::letp solution</span>
d %>%<span> </span>replyr::<span>letp</span>(<span>alias =</span> <span>list</span>(<span>cname =</span> cname, <span>rname =</span> rname),
                   <span>expr =</span> {
                   . %>%<span> </span><span>mutate</span>(<span>rname =</span> <span>is.na</span>(cname))
                   })
 <span># x x_isNA</span>
 <span># 1 1 FALSE</span>
 <span># 2 NA TRUE</span></code></pre>
</div>
<p>The alias map is deliberately only allowed to be a string to string map (no environments, <code>as.name</code>, <code>formula</code>, expressions, or values) so <code>replyr::let</code> <em>itself</em> is easy to use in automation or program over. I’ll repeat that for emphasis: externally <code>replyr::let</code> is completely controllable through standard (or parametric) evaluation interfaces. Also notice the code we wrote is never directly mentions “<code>x</code>” or “<code>x_isNA</code>” as it pulls these names out of its execution environment.</p>
<p>All of these solutions have consequences and corner cases. Our (biased) opinion is: we dislike <code>replyr::let</code> the least.</p>
</div>
<div>
<h2>More reading</h2>
<p>Our group has been writing <em>a lot</em> on <code>replyr::let</code>. It is new code, yet something we think analysts should try. Some of our recent notes include:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/12/parametric-variable-names-and-dplyr/">The original proposal</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/12/using-replyrlet-to-parameterize-dplyr-expressions/">A non-trivial example</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/12/helplet-packagereplyr/">The <code>replyr::let</code> help documentation</a></li>
<li><a target="_blank" rel="nofollow" href="https://github.com/WinVector/replyr/blob/master/README.md">The <code>replyr</code> package README</a>.</li>
</ul>
</div>
<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.win-vector.com/blog/2016/12/comparative-examples-using-replyrlet/"> R – Win-Vector Blog</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/uthE-3ejdh8"></a></div></center>