<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/recode-values-with-character-subsetting/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tjmahr.github.io/recode-values-with-character-subsetting/"> Higher Order Functions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Do you ever have to recode many values at once? It’s a frequent chore when<br>
preparing data. For example, suppose we had to replace state abbreviations<br>
with the full names:</p>
<div>
<div>
<pre><code><span>abbs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;AL&quot;</span><span>,</span><span> </span><span>&quot;AK&quot;</span><span>,</span><span> </span><span>&quot;AZ&quot;</span><span>,</span><span> </span><span>&quot;AZ&quot;</span><span>,</span><span> </span><span>&quot;WI&quot;</span><span>,</span><span> </span><span>&quot;WS&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>You could write several <code>ifelse()</code> statements.</p>
<div>
<div>
<pre><code><span>ifelse</span><span>(</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AL&quot;</span><span>,</span><span> </span><span>&quot;Alabama&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AK&quot;</span><span>,</span><span> </span><span>&quot;Alaska&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AZ&quot;</span><span>,</span><span> </span><span>&quot;Arizona&quot;</span><span>,</span><span> </span></code></pre>
</div>
</div>
<p>Actually, never mind! That gets out of hand very quickly.</p>
<p><code>case_when()</code> is nice, especially when the replacement rules are more complex<br>
than 1-to-1 matching.</p>
<div>
<div>
<pre><code><span>dplyr</span><span>::</span><span>case_when</span><span>(</span><span>
</span><span># Syntax: logical test ~ value to use when test is TRUE</span><span>
</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AL&quot;</span><span> </span><span>~</span><span> </span><span>&quot;Alabama&quot;</span><span>,</span><span>
</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AK&quot;</span><span> </span><span>~</span><span> </span><span>&quot;Alaska&quot;</span><span>,</span><span>
</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;AZ&quot;</span><span> </span><span>~</span><span> </span><span>&quot;Arizona&quot;</span><span>,</span><span>
</span><span>abbs</span><span> </span><span>==</span><span> </span><span>&quot;WI&quot;</span><span> </span><span>~</span><span> </span><span>&quot;Wisconsin&quot;</span><span>,</span><span>
</span><span># a fallback/default value</span><span>
</span><span>TRUE</span><span> </span><span>~</span><span> </span><span>&quot;No match&quot;</span><span>
</span><span>)</span><span>
</span><span>#> [1] &quot;Alabama&quot; &quot;Alaska&quot; &quot;Arizona&quot; &quot;Arizona&quot; &quot;Wisconsin&quot; &quot;No match&quot;</span><span>
</span></code></pre>
</div>
</div>
<p>We could also use one of my very favorite R tricks:<br>
<a rel="nofollow" href="http://adv-r.had.co.nz/Subsetting.html#applications" target="_blank"><strong>Character subsetting</strong></a>.<br>
We create a named vector where the names are the data we have and the values are<br>
the data we want. I use the mnemonic <code>old_value = new_value</code>. In this case, we<br>
make a lookup table like so:</p>
<div>
<div>
<pre><code><span>lookup</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>
</span><span># Syntax: name = value</span><span>
</span><span>&quot;AL&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Alabama&quot;</span><span>,</span><span>
</span><span>&quot;AK&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Alaska&quot;</span><span>,</span><span>
</span><span>&quot;AZ&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Arizona&quot;</span><span>,</span><span>
</span><span>&quot;WI&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Wisconsin&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>For example, subsetting with the string <code>&quot;AL&quot;</code> will retrieve the value with the<br>
name <code>&quot;AL&quot;</code>.</p>
<div>
<div>
<pre><code><span>lookup</span><span>[</span><span>&quot;AL&quot;</span><span>]</span><span>
</span><span>#> AL </span><span>
</span><span>#> &quot;Alabama&quot;</span><span>
</span></code></pre>
</div>
</div>
<p>With a vector of names, we can look up the values all at once.</p>
<div>
<div>
<pre><code><span>lookup</span><span>[</span><span>abbs</span><span>]</span><span>
</span><span>#> AL AK AZ AZ WI </span></code></pre></div></div>