<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/wrapr-for-sweet-r-code/"></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/03/wrapr-for-sweet-r-code/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This article is on writing sweet <a target="_blank" rel="nofollow" href="https://cran.r-project.org/"><code>R</code></a> code using the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/wrapr/index.html"><code>wrapr</code> package</a>.</p>
<p><center><br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/wrapr/index.html"><img title="wrapr.png" alt="Wrapr" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/03/wrapr.png?resize=600%2C446" border="0" data-recalc-dims="1"></a><br>
</center><span></span></p>
<p>
<h2>The problem</h2>
<p>Consider the following <a target="_blank" rel="nofollow" href="https://cran.r-project.org/"><code>R</code></a> puzzle. You are given: a <code>data.frame</code>, the name of a column that you wish to find missing values (<code>NA</code>) in, and the name of a column to land the result. For instance:</p>
<pre>
d &lt;- data.frame(x = c(1, NA))
print(d)

 #    x
 # 1  1
 # 2 NA

cname &lt;- 'x'
print(cname)

 # [1] &quot;x&quot;

rname &lt;- paste(cname, 'isNA', sep = '_')
print(rname)

 # [1] &quot;x_isNA&quot;
</pre>
<p>How do you write generic code to populate the column <code>x_isNA</code> with which rows of <code>x</code> are missing?</p>
<h3>The “base R” solution</h3>
<p>In “base <code>R</code>” (R without additional packages) this is easy.</p>
<p>When you know the column names while writing the code:</p>
<pre>
d2 &lt;- d
d2$x_isNA &lt;- is.na(d2$x)

print(d2)

 #    x x_isNA
 # 1  1  FALSE
 # 2 NA   TRUE
</pre>
<p>And when you don’t know the column names while writing the code (but know they will arrive in variables later):</p>
<pre>
d2 &lt;- d
d2[[rname]] &lt;- is.na(d2[[cname]])
</pre>
<p>The “base R” solution really is quite elegant.</p>
<h3>The “all in” non-standard evaluation <code>dplyr::mutate</code> solution</h3>
<p>As far as I can tell the “all in” <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html">non-standard evaluation</a> <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr"><code>dplyr::mutate</code></a> solution is something like the following.</p>
<p>When you know the column names while writing the code:</p>
<pre>
library(&quot;dplyr&quot;)
d %>% mutate(x_isNA = is.na(x))
</pre>
<p>And when you don’t know the column names while writing the code (but know they will arrive in variables later):</p>
<pre>
d %>%
  mutate_(.dots =
            stats::setNames(list(lazyeval::interp(
              ~ is.na(VAR),
              VAR = as.name(cname)
            )),
            rname))
</pre>
<h3>The sweet <code>wrapr::let</code> <code>dplyr::mutate</code> solution</h3>
<p>We will only work the “when you don’t yet know the column name” (or parametric) version:</p>
<pre>
library(&quot;wrapr&quot;)
let(list(COL = cname, RES = rname),
    d %>% mutate(RES = is.na(COL))
)
</pre>
<p>I think that this is pretty sweet, and can really level up your <code>dplyr</code> game.</p>
<p><center><br>
<iframe height="315" width="560" allowfullscreen src="https://www.youtube.com/embed/nG9fXbhoPJE"></iframe><br>
</center></p>
<p>If function behavior depends on variable names, then convenient control of functions is eventually going to require convenient control of variable names; so needing to re-map at some point is inevitable.</p>
<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/2017/03/wrapr-for-sweet-r-code/"> 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/D_e-p1Us2GI"></a></div>