Description Usage Arguments Value Note Examples
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.
1 2 3 4 5 6 7 |
x |
a |
wt |
(Optional) If omitted (and no variable named |
sort |
if |
... |
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 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 can call rename()
on the result,
or write the summarise()
yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # tally() is short-hand for summarise()
mtcars %>% tally()
mtcars %>% group_by(cyl) %>% 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
species %>% count(species, sort = TRUE)
# Use rename() to change the name of the newly created column:
species <-
starwars %>%
count(species, homeworld, sort = TRUE) %>%
rename(n_species_by_homeworld = n)
species
species %>%
count(species, sort = TRUE) %>%
rename(n_species = n)
# add_count() is useful for groupwise filtering
# e.g.: show details for species that have a single member
starwars %>%
add_count(species) %>%
filter(n == 1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.