Description Usage Arguments Value Examples
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 lowerlevel 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 groupwise counts.
1 2 3 4 5 6 7 
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
(nonmissing) values of variable 
sort 

name 

A data.frame
. count()
and add_count()
have the same groups as the input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  # 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 lowerlevel 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)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.