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

If omitted, will count the number of rows. If specified, will perform a "weighted" count by summing the (non-missing) values of variable wt. If omitted, and column n exists, it will automatically be used as a weighting variable, although you will have to specify name to provide a new name for the output.

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 there's already a column called n, it will error, and require you to specify the 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 Nov. 2, 2023, 5:27 p.m.