count: Count observations by group

View source: R/count-tally.R

countR Documentation

Count observations by group

Description

count() lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()). count() is paired with tally(), a lower-level helper that is equivalent to df %>% summarise(n = n()). Supply wt to perform weighted counts, switching the summary from from n = n() to n = sum(wt). add_count() and add_tally() are equivalent to count() and tally() but use mutate() instead of summarise() so that they add a new column with group-wise counts.

Usage

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

tally(x, wt = NULL, sort = FALSE, name = NULL)

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

add_tally(x, wt = NULL, sort = FALSE, name = NULL)

Arguments

x

A data.frame.

...

Variables to group by.

wt

Frequency weights. If NULL, the default, the rows of each group are counted. Otherwise, the weighted count of each group is the sum of the (non-missing) values of wt, which may be a column or an expression of columns and variables of the calling environment, e.g. wt = price * k. A column named n is not used as a weight unless it is given as wt.

sort

logical(1). If TRUE, will show the largest groups at the top.

name

character(1). The name of the new column in the output. If omitted, it will default to n. If n would clash with a column that the output keeps, i.e. a grouping variable for count() and tally() or any column of the input for add_count() and add_tally(), n is repeated until the name is unique (nn, nnn, ...) and a message gives the name used. A column n that is not kept, e.g. a column summarised away by count(), does not clash, and the count is stored in n. An explicit name is used as given and replaces any column of that name.

Value

A data.frame. count() and add_count() have the same groups as the input.

Examples

# count() is a convenient way to get a sense of the distribution of
# values in a dataset
mtcars %>% count(cyl)
mtcars %>% count(cyl, sort = TRUE)
mtcars %>% count(cyl, am, sort = TRUE)
# Note that if the data are already grouped, count() adds an additional grouping variable
# which is removed afterwards
mtcars %>% group_by(gear) %>% count(cyl)

# tally() is a lower-level function that assumes you've done the grouping
mtcars %>% tally()
mtcars %>% group_by(cyl) %>% tally()

# both count() and tally() have add_ variants that work like mutate() instead of summarise
mtcars %>% add_count(cyl, wt = am)
mtcars %>% add_tally(wt = am)


poorman documentation built on Oct. 29, 2026, 1:07 a.m.