Count/tally observations by group

tally() is a convenient wrapper for summarise that will either call n() or sum(n) depending on whether you're tallying for the first time, or re-tallying. count() is similar but calls group_by() before and ungroup() after.

add_tally() adds a column n to a table based on the number of items within each existing group, while add_count() is a shortcut that does the grouping as well. These functions are to tally() and count() as mutate() is to summarise(): they add an additional column rather than collapsing each group.

tally(x, wt, sort = FALSE)

count(x, ..., wt = NULL, sort = FALSE)

add_tally(x, wt, sort = FALSE)

add_count(x, ..., wt = NULL, sort = FALSE)


a tbl() to tally/count.


(Optional) If omitted (and no variable named n exists in the data), will count the number of rows. If specified, will perform a "weighted" tally by summing the (non-missing) values of variable wt. A column named n (but not nn or nnn) will be used as weighting variable by default in tally(), but not in count(). This argument is automatically quoted and later evaluated in the context of the data frame. It supports unquoting. See vignette("programming") for an introduction to these concepts.


if TRUE will sort output in descending order of n


Variables to group by.


A tbl, grouped the same way as x.


The column name in the returned data is usually n, even if you have supplied a weight.

If the data already already has a column named n, the output column will be called nn. If the table already has columns called n and nn then the column returned will be nnn, and so on.

There is currently no way to control the output variable name - if you need to change the default, you'll have to write the summarise() yourself.

  • tally
  • count
  • add_tally
  • add_count
# tally() is short-hand for summarise()
mtcars %>% tally()
# count() is a short-hand for group_by() + tally()
mtcars %>% count(cyl)

# add_tally() is short-hand for mutate()
mtcars %>% add_tally()
# add_count() is a short-hand for group_by() + add_tally()
mtcars %>% add_count(cyl)

# count and tally are designed so that you can call
# them repeatedly, each time rolling up a level of detail
species <- starwars %>% count(species, homeworld, sort = TRUE)
species %>% count(species, sort = TRUE)

# add_count() is useful for groupwise filtering
# e.g.: show only species that have a single member
starwars %>%
  add_count(species) %>%
  filter(n == 1)
# }
Documentation reproduced from package dplyr, version 0.7.8, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.