dplyr::count()
   get_help() docs


Description

The count() function is part of the {dplyr} package, which is part of the {tidyverse}.

We use this function to create a new column (named n by default) in a tibble (data frame) that counts the number of rows in specified group. It is a convenient shortcut for: group_by(column_name) %>% tally() %>% ungroup()

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::count() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::count()

Conceptual Usage

tibble %>% 
  count(column to count)

tibble %>% 
  count(first column to count,
        second column to count in all combinations with the first,
        etc. as needed)

tibble %>%
  count(column to count, 
        name = "desired custom name for output column")

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Count the number of each vore category in `msleep`
msleep %>%
  count(vore)
## # A tibble: 5 × 2
##   vore        n
##   <chr>   <int>
## 1 carni      10
## 2 herbi      24
## 3 insecti     4
## 4 omni       18
## 5 <NA>        5


# Count the number of each combination of `vore` and `order` categories in `msleep`
msleep %>%
  count(vore, order)
## # A tibble: 28 × 3
##    vore  order               n
##    <chr> <chr>           <int>
##  1 carni Carnivora           7
##  2 carni Cetacea             1
##  3 carni Cingulata           1
##  4 carni Didelphimorphia     1
##  5 herbi Artiodactyla        4
##  6 herbi Diprotodontia       1
##  7 herbi Hyracoidea          2
##  8 herbi Lagomorpha          1
##  9 herbi Perissodactyla      3
## 10 herbi Pilosa              1
## # … with 18 more rows
# Count the number of each vore category in `msleep` but name the output column `num_vore` instead of the default `n`
msleep %>%
  count(vore, name = "num_vore")
## # A tibble: 5 × 2
##   vore    num_vore
##   <chr>      <int>
## 1 carni         10
## 2 herbi         24
## 3 insecti        4
## 4 omni          18
## 5 <NA>           5