Streamline your dplyr chains with count()

If you frequently use chains like this

data %>%
  group_by(var1, var2) %>%
  summarise(n = n())

consider using

count(data, var1, var2)

instead. It produces the same output, you need to type less and the code is more expressive.

By default, the column containing the count will be named n. Want to change that? Simply set the name parameter of count(), e.g.

data(iris)
dplyr::count(iris, Species, name = "n_flowers")
## # A tibble: 3 x 2
##   Species    n_flowers
##   <fct>          <int>
## 1 setosa            50
## 2 versicolor        50
## 3 virginica         50

Thomas Neitmann

dplyr

91 Words

2020-03-06 00:00 +0700

53b1dba @ 2020-03-22