dplyr::tally()
get_help()
docs
The tally()
function is part of the {dplyr}
package, which is part of the {tidyverse}
.
The function tally()
creates a new column (named n
by default) in a tibble (data frame) that tallies the number of rows (observations) in specified group. If there is no grouping, tally()
will give the total number of rows. This is similar to using base nrow()
except, like many {dplyr}
verbs, tally()
returns a tibble.
Note that the {dplyr}
function count()
provides a shortcut for group_by() %>% tally() %>% ungroup()
.
To use this function, you need to either first load the {dplyr}
library, or always use the function with dplyr::tally()
notation.
# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::tally() dplyr
# Summarize into tibble indicating total rows
%>%
tibble tally()
%>%
tibble tally(name = count) # specify a non-default output column name
%>%
tibble group_by(grouping column) %>%
tally() %>%
ungroup()
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 total number of rows in msleep
%>%
msleep tally()
## # A tibble: 1 × 1
## n
## <int>
## 1 61
# Count the total number of rows in msleep and specify column name
%>%
msleep tally(name = 'total')
## # A tibble: 1 × 1
## total
## <int>
## 1 61
# Count total number of rows for each vore.
# Note that this can also be done more simply with `dplyr::count()`
%>%
msleep group_by(vore) %>%
tally()
## # A tibble: 5 × 2
## vore n
## <chr> <int>
## 1 carni 10
## 2 herbi 24
## 3 insecti 4
## 4 omni 18
## 5 <NA> 5
# Subset carnivores to keep only rows where the genus is _either_ 'Panthera' or 'Canis'
# In other words, it should be TRUE that the genus is %in% the array c("Panthera", "Canis").
# Remember: We do _not_ use `==` when asking if a value is in an array.
%>%
carnivores tally(genus %in% c("Panthera", "Canis"))
## # A tibble: 1 × 1
## n
## <int>
## 1 4