This blog post is an update to an older one I wrote in March. In the post from March, dplyr 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 dplyr 0.70.
Create new columns with mutate() and case_when()
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 case_when(). First, load dplyr and the mtcars dataset:
library(&quot;dplyr&quot;)
data(mtcars)
This was how it was done in version 0.50 (notice the ‘.$’ symbol before the variable ‘carb’):
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)
## 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
This has been simplified to:
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)
## 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
No need for .$ anymore.
Apply a function to certain columns only, by rows, with purrrlyr
dplyr wasn’t the only package to get an overhaul, purrr also got the same treatment.
In the past, I applied a function to certains columns like this:
mtcars %__%
select(am, gear, carb) %__%
purrr::by_row(sum, .collate = &quot;cols&quot;, .to = &quot;sum_am_gear_carb&quot;) -__ mtcars2
head(mtcars2)
Now, by_row() does not exist in purrr anymore, but instead a new package called purrrlyr was introduced with functions that don’t really fit inside purrr nor dplyr:
mtcars %__%
select(am, gear, carb) %__%
purrrlyr::by_row(sum, .collate = &quot;cols&quot;, .to = &quot;sum_am_gear_carb&quot;) -__ mtcars2
head(mtcars2)
## # A tibble: 6 x 4
## am gear carb sum_am_gear_carb
## ## 1 1 4 4 9
## 2 1 4 4 9
## 3 1 4 1 6
## 4 0 3 1 4
## 5 0 3 2 5
## 6 0 3 1 4
Think of purrrlyr as purrrs and dplyrs love child.
Using dplyr functions inside your own functions, or what is tidyeval
Programming with dplyr has been simplified a lot. Before version 0.70, one needed to use dplyr in conjuction with lazyeval to use dplyr functions inside one’s own fuctions. It was not always very easy, especially if you mixed columns and values inside your functions. Here’s the example from the March blog post:
extract_vars %
select_(lazyeval::interp(~contains(some_string))) -__ data
return(data)
}
extract_vars(mtcars, &quot;spam&quot;)
More examples are available in this other blog post.
I will revisit them now with dplyr’s new tidyeval syntax. I’d recommend you read the Tidy evaluation vignette here. This vignette is part of the rlang package, which gets used under the hood by dplyr for all your programming needs. Here is the function I called simpleFunction(), written with the old dplyr syntax:
simpleFunction %
group_by_(col_name) %__%
summarise(mean_mpg = mean(mpg)) -__ dataset
return(dataset)
}
simpleFunction(mtcars, &quot;cyl&quot;)
## # A tibble: 3 x 2
## cyl mean_mpg
## ## 1 4 26.66364
## 2 6 19.74286
## 3 8 15.10000
With the new synax, it must be rewritten a little bit:
simpleFunction %
summarise(mean_mpg = mean(mpg)) -__ dataset
return(dataset)
}
simpleFunction(mtcars, cyl)
## # A tibble: 3 x 2
## cyl mean_mpg
## ## 1 4 26.66364
## 2 6 19.74286
## 3 8 15.10000
What has changed? Forget the underscore versions of the usual functions such as select_(), group_by_(), etc. Now, you must quote the column name using enquo() (or just quo() if working interactively, outside a function), which returns a quosure. This quosure can then be evaluated using !! in front of the quosure and inside the usual dplyr functions.
Let’s look at another example:
simpleFunction %
summarise(mean_cyl = mean(cyl)) -__ dataset
return(dataset)
}
simpleFunction(mtcars, &quot;am&quot;, 1)
## mean_cyl
## 1 5.076923
As you can see, it’s a bit more complicated, as you needed to use lazyeval::interp() to make it work. With the improved dplyr, here’s how it’s done:
simpleFunction %
summarise(mean_cyl = mean(cyl)) -__ dataset
return(dataset)
}
simpleFunction(mtcars, am, 1)
## mean_cyl
## 1 5.076923
Much, much easier! There is something that you must pay attention to though. Notice that I’ve written:
filter((!!col_name) == value)
and not:
filter(!!col_name == value)
I have enclosed !!col_name inside parentheses. I struggled with this, but thanks to help from @dmi3k and @_lionelhenry I was able to understand what was happening (isn’t the #rstats community on twitter great?).
One last thing: let’s make this function a bit more general. I hard-coded the variable cyl inside the body of the function, but maybe you’d like the mean of another variable? Easy:
simpleFunction dataset
return(dataset)
}
simpleFunction(mtcars, am, cyl, 1)
## mean((cyl))
## 1 5.076923
«That’s very nice Bruno, but mean((cyl)) in the output looks ugly as sin» you might think, and you’d be right. It is possible to set the name of the column in the output using := instead of =:
simpleFunction