<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/let-xx-in-r/"></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/11/let-xx-in-r/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Our article <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/lets-have-some-sympathy-for-the-part-time-r-user/" target="_blank">&quot;Let’s Have Some Sympathy For The Part-time R User&quot;</a> includes two points:</p>
<ul>
<li>Sometimes you have to write parameterized or re-usable code.</li>
<li>The methods for doing this should be easy and legible.</li>
</ul>
<p>The first point feels abstract, until you find yourself wanting to re-use code on new projects. As for the second point: I feel the <a rel="nofollow" href="https://winvector.github.io/wrapr/" target="_blank"><code>wrapr</code></a> package is the easiest, safest, most consistent, and most legible way to achieve maintainable code re-use in <a rel="nofollow" href="https://cran.r-project.org/" target="_blank"><code>R</code></a>.</p>
<p>In this article we will show how <code>wrapr</code> makes code-rewriting even easier with its new <code>let x=x</code> automation.</p>
<div>
<center><br>
<img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/11/411gJqs4qlL.jpg?resize=249%2C220" alt="411gJqs4qlL" title="411gJqs4qlL.jpg" data-recalc-dims="1">
<p>Let X=X</p>
<p>

<p><span></span></p>
<p>There are <em>very</em> important reasons to choose a package that makes things easier. One is debugging:</p>
<blockquote>
<p>
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it?</p>
<p>
<p><a rel="nofollow" href="https://en.wikiquote.org/wiki/Brian_Kernighan" target="_blank">Brian Kernighan, <em>The Elements of Programming Style</em>, 2nd edition, chapter 2</a></p>
</blockquote>
<p>Let’s take the monster example from <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/lets-have-some-sympathy-for-the-part-time-r-user/" target="_blank">&quot;Let’s Have Some Sympathy For The Part-time R User&quot;</a>.</p>
<p>The idea was that perhaps one had worked out a complicated (but useful and important) by-hand survey scoring method:</p>
<div>
<pre><code><span>suppressPackageStartupMessages</span>(<span>library</span>(<span>&quot;dplyr&quot;</span>))
<span>library</span>(<span>&quot;wrapr&quot;</span>)

d &lt;-<span> </span><span>data.frame</span>(
  <span>subjectID =</span> <span>c</span>(<span>1</span>,                   
                <span>1</span>,
                <span>2</span>,                   
                <span>2</span>),
  <span>surveyCategory =</span> <span>c</span>(
    <span>'withdrawal behavior'</span>,
    <span>'positive re-framing'</span>,
    <span>'withdrawal behavior'</span>,
    <span>'positive re-framing'</span>
  ),
  <span>assessmentTotal =</span> <span>c</span>(<span>5</span>,                 
                      <span>2</span>,
                      <span>3</span>,                  
                      <span>4</span>),
  <span>stringsAsFactors =</span> <span>FALSE</span>
)

scale &lt;-<span> </span><span>0.237</span>

d %>%
<span> </span><span>group_by</span>(subjectID) %>%
<span> </span><span>mutate</span>(<span>probability =</span>
           <span>exp</span>(assessmentTotal *<span> </span>scale)/
<span> </span><span>sum</span>(<span>exp</span>(assessmentTotal *<span> </span>scale))) %>%
<span> </span><span>arrange</span>(probability, surveyCategory) %>%
<span> </span><span>mutate</span>(<span>isDiagnosis =</span> <span>row_number</span>() ==<span> </span><span>n</span>()) %>%
<span> </span><span>filter</span>(isDiagnosis) %>%
<span> </span><span>ungroup</span>() %>%
<span> </span><span>select</span>(subjectID, surveyCategory, probability) %>%
<span> </span><span>rename</span>(<span>diagnosis =</span> surveyCategory) %>%
<span> </span><span>arrange</span>(subjectID)</code></pre>
</div>
<pre><code>## # A tibble: 2 x 3
##   subjectID           diagnosis probability
##       </code></pre></center></div>