count {poorman} | R 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 |
... |
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 |
sort |
|
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)