tally: Count/tally observations by group

Description Usage Arguments Value Note Examples

View source: R/count-tally.R

Description

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.

Usage

1
2
3
4
5
6
7
tally(x, wt, sort = FALSE)

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

add_tally(x, wt, sort = FALSE)

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

Arguments

x

a tbl() to tally/count.

wt

(Optional) If omitted, will count the number of rows. If specified, will perform a "weighted" tally by summing the (non-missing) values of variable wt. This argument is automatically quoted and later evaluated in the context of the data frame. It supports unquoting. See vignette("programming") for an introduction to these concepts.

sort

if TRUE will sort output in descending order of n

...

Variables to group by.

Value

A tbl, grouped the same way as x.

Note

The column name in the returned data is usually n, even if you have supplied a weight.

If the data already 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'll have to write the summarise() yourself.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# tally() is short-hand for summarise()
mtcars %>% 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)

# add_count() is useful for groupwise filtering
# e.g.: show only species that have a single member
starwars %>%
  add_count(species) %>%
  filter(n == 1)

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

   n
1 32
# A tibble: 3 x 2
    cyl     n
  <dbl> <int>
1     4    11
2     6     7
3     8    14
# A tibble: 32 x 12
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb     n
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
 1  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4    32
 2  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4    32
 3  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1    32
 4  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1    32
 5  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2    32
 6  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1    32
 7  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4    32
 8  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2    32
 9  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2    32
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    32
# ... with 22 more rows
# A tibble: 32 x 12
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb     n
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
 1  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4     7
 2  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4     7
 3  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1    11
 4  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1     7
 5  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2    14
 6  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1     7
 7  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4    14
 8  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2    11
 9  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2    11
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4     7
# ... with 22 more rows
# A tibble: 58 x 3
    species homeworld     n
      <chr>     <chr> <int>
 1    Human  Tatooine     8
 2    Human     Naboo     5
 3    Human      <NA>     5
 4   Gungan     Naboo     3
 5    Human  Alderaan     3
 6    Droid  Tatooine     2
 7    Droid      <NA>     2
 8    Human  Corellia     2
 9    Human Coruscant     2
10 Kaminoan    Kamino     2
# ... with 48 more rows
# A tibble: 38 x 2
    species    nn
      <chr> <int>
 1    Human    16
 2    Droid     3
 3     <NA>     3
 4   Zabrak     2
 5   Aleena     1
 6 Besalisk     1
 7   Cerean     1
 8 Chagrian     1
 9 Clawdite     1
10      Dug     1
# ... with 28 more rows
# A tibble: 29 x 14
                    name height  mass hair_color       skin_color eye_color
                   <chr>  <int> <dbl>      <chr>            <chr>     <chr>
 1                Greedo    173    74       <NA>            green     black
 2 Jabba Desilijic Tiure    175  1358       <NA> green-tan, brown    orange
 3                  Yoda     66    17      white            green     brown
 4                 Bossk    190   113       none            green       red
 5                Ackbar    180    83       none     brown mottle    orange
 6 Wicket Systri Warrick     88    20      brown            brown     brown
 7             Nien Nunb    160    68       none             grey     black
 8           Nute Gunray    191    90       none    mottled green       red
 9                 Watto    137    NA      black       blue, grey    yellow
10               Sebulba    112    40       none        grey, red    orange
# ... with 19 more rows, and 8 more variables: birth_year <dbl>, gender <chr>,
#   homeworld <chr>, species <chr>, films <list>, vehicles <list>,
#   starships <list>, n <int>

dplyr documentation built on Sept. 29, 2017, 1:03 a.m.