<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/lesser-known-dplyr-0-7-tricks/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2017-06-19-dplyr-0-70-tutorial/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><br>
</p>
<p><br>
</p>
<p>This blog post is an update to an older <a rel="nofollow" href="http://www.brodrigues.co/blog/2017-02-17-lesser_known_tricks/" target="_blank">one</a> I wrote in March. In the post from March, <code>dplyr</code> was at version 0.50, but since then a major update introduced some changes that make some of the tips in that post obsolete. So here I revisit the blog post from March by using <code>dplyr</code> 0.70.</p>
<div>
<h2>Create new columns with <code>mutate()</code> and <code>case_when()</code></h2>
<p>The basic things such as selecting columns, renaming them, filtering, etc did not change with this new version. What did change however is creating new columns using <code>case_when()</code>. First, load <code>dplyr</code> and the <code>mtcars</code> dataset:</p>
<pre><code>library(&quot;dplyr&quot;)
data(mtcars)</code></pre>
<p>This was how it was done in version 0.50 (notice the ‘.$’ symbol before the variable ‘carb’):</p>
<pre><code>mtcars %>% 
    mutate(carb_new = case_when(.$carb == 1 ~ &quot;one&quot;,
                                .$carb == 2 ~ &quot;two&quot;,
                                .$carb == 4 ~ &quot;four&quot;,
                                 TRUE ~ &quot;other&quot;)) %>% 
    head(5)</code></pre>
<pre><code>##    mpg cyl disp  hp drat    wt  qsec vs am gear carb carb_new
## 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     four
## 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     four
## 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1      one
## 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1      one
## 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      two</code></pre>
<p>This has been simplified to:</p>
<pre><code>mtcars %>% 
    mutate(carb_new = case_when(carb == 1 ~ &quot;one&quot;,
                                carb == 2 ~ &quot;two&quot;,
                                carb == 4 ~ &quot;four&quot;,
                                TRUE ~ &quot;other&quot;)) %>% 
    head(5)</code></pre>
<pre><code>##    mpg cyl disp  hp drat    wt  qsec vs am gear carb carb_new
## 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     four
## 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     four
## 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1      one
## 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1      one
## 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      two</code></pre>
<p>No need for <code>.$</code> anymore.</p>
</div>
<div>
<h2>Apply a function to certain columns only, by rows, with <code>purrrlyr</code></h2>
<p><code>dplyr</code> wasn’t the only package to get an overhaul, <code>purrr</code> also got the same treatment.</p>
<p>In the past, I applied a function to certains columns like this:</p>
<pre><code>mtcars %>%
    select(am, gear, carb) %>%
    purrr::by_row(sum, .collate = &quot;cols&quot;, .to = &quot;sum_am_gear_carb&quot;) -> mtcars2
head(mtcars2)</code></pre>
<p>Now, <code>by_row()</code> does not exist in <code>purrr</code> anymore, but instead a new package called <code>purrrlyr</code> was introduced with functions that don’t really fit inside <code>purrr</code> nor <code>dplyr</code>:</p>
<pre><code>mtcars %>%
    select(am, gear, carb) %>%
    purrrlyr::by_row(sum, .collate = &quot;cols&quot;, .to = &quot;sum_am_gear_carb&quot;) -> mtcars2
head(mtcars2)</code></pre>
<pre><code>## # A tibble: 6 x 4
##      am  gear  carb sum_am_gear_carb
##   </code></pre></div>