Count

The function count() counts the number of observations/rows for each unique value within one or more variables. I.e. you can count the number of rows each group represents.

Tidyverse reference page

Dataset

For demonstration we’ll load the amphibian_div_tbl data from the mgrtibbles package (hyperlink includes install instructions).

#Load package
library("mgrtibbles")
#amphibian_div_tbl tibble for demonstration
mgrtibbles::amphibian_div_tbl
# A tibble: 138 × 15
   Species                  IUCN.Red.List.Status iucn_2cat Order   Family  Genus
   <chr>                    <fct>                <fct>     <chr>   <chr>   <chr>
 1 Acris blanchardi         <NA>                 <NA>      Anura   Hylidae Acris
 2 Acris crepitans          Least Concern (LC)   LC        Anura   Hylidae Acris
 3 Acris gryllus            Least Concern (LC)   LC        Anura   Hylidae Acris
 4 Ambystoma barbouri       Near Threatened (NT) nonLC     Caudata Ambyst… Amby…
 5 Ambystoma jeffersonianum Least Concern (LC)   LC        Caudata Ambyst… Amby…
 6 Ambystoma laterale       Least Concern (LC)   LC        Caudata Ambyst… Amby…
 7 Ambystoma macrodactylum  Least Concern (LC)   LC        Caudata Ambyst… Amby…
 8 Ambystoma maculatum      Least Concern (LC)   LC        Caudata Ambyst… Amby…
 9 Ambystoma texanum        Least Concern (LC)   LC        Caudata Ambyst… Amby…
10 Ambystoma tigrinum       Least Concern (LC)   LC        Caudata Ambyst… Amby…
# ℹ 128 more rows
# ℹ 9 more variables: Age_at_maturity_min_y <dbl>, Age_at_maturity_max_y <dbl>,
#   Body_size_mm <dbl>, Longevity_max_y <dbl>, Litter_size_min_n <dbl>,
#   Litter_size_max_n <dbl>, Offspring_size_min_mm <dbl>,
#   Offspring_size_max_mm <fct>, Development <chr>

One variable/column

The count() function will count the number of each unique value within one or more variables.

amphibian_div_tbl |> dplyr::count(IUCN.Red.List.Status)
# A tibble: 7 × 2
  IUCN.Red.List.Status                 n
  <fct>                            <int>
1 Least Concern (LC)                  94
2 Near Threatened (NT)                12
3 Data Deficient (DD)                  8
4 Vulnerable (VU)                     18
5 Endangered (EN)                      4
6 Least Concern (LC) - Provisional     1
7 <NA>                                 1

The count() function works on grouped tibbles, counting the unique values within the group.

amphibian_div_tbl |> 
    #Group by IUCN.Red.List.Status
    dplyr::group_by(IUCN.Red.List.Status) |>
    #Count will count grouped info
    dplyr::count()
# A tibble: 7 × 2
# Groups:   IUCN.Red.List.Status [7]
  IUCN.Red.List.Status                 n
  <fct>                            <int>
1 Least Concern (LC)                  94
2 Near Threatened (NT)                12
3 Data Deficient (DD)                  8
4 Vulnerable (VU)                     18
5 Endangered (EN)                      4
6 Least Concern (LC) - Provisional     1
7 <NA>                                 1

Multiple variables/columns

It also works when specifying multiple variables/columns to use together.

amphibian_div_tbl |> dplyr::count(Order, Family, Genus)
# A tibble: 26 × 4
   Order   Family         Genus          n
   <chr>   <chr>          <chr>      <int>
 1 Anura   Ascaphidae     Ascaphus       2
 2 Anura   Bufonidae      Anaxyrus       4
 3 Anura   Bufonidae      Incilius       1
 4 Anura   Hylidae        Acris          3
 5 Anura   Hylidae        Hyla           7
 6 Anura   Hylidae        Pseudacris     6
 7 Anura   Ranidae        Rana          14
 8 Anura   Scaphiopodidae Spea           2
 9 Caudata Ambystomatidae Ambystoma      7
10 Caudata Amphiumidae    Amphiuma       3
# ℹ 16 more rows