Skip to Tutorial Content

Counts and Tallies of Variables in a dataset

Sometimes it is convenient to to have a total tally() (denominator) of your dataset, or by current groups. This helps in calculating proportions or percentages. You can also perform counts by groups and subgroups. The simplest way to count is with the n() function, which counts the observations and places the result in a vector named (by default) “n”.

We will start with the laryngoscope dataset, seen here with glimpse():

glimpse(laryngoscope)
## Rows: 99
## Columns: 22
## $ age                      <dbl> 51, 52, 37, 20, 35, 39, 52, 59, 32, 24, 45, 2…
## $ gender                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, …
## $ asa                      <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, …
## $ bmi                      <dbl> 56.20, 44.60, 41.60, 46.32, 61.00, 44.00, 44.…
## $ mallampati               <dbl> 1, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 3, 1, 2, …
## $ randomization            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ attempt1_time            <dbl> 29.00, 29.00, 31.00, 31.00, 21.00, 10.00, 13.…
## $ attempt1_s_f             <dbl> 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ attempt2_time            <dbl> NA, NA, 60, 46, NA, NA, NA, NA, NA, NA, NA, N…
## $ attempt2_assigned_method <dbl> NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ attempt2_s_f             <dbl> NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ attempt3_time            <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ attempt3_assigned_method <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ attempt3_s_f             <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ attempts                 <dbl> 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ failures                 <dbl> 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ total_intubation_time    <dbl> 29.00, 29.00, 91.00, 77.00, 21.00, 10.00, 13.…
## $ intubation_overall_s_f   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ bleeding                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ ease                     <dbl> 5, 20, 80, 80, 20, 2, 10, 60, 15, 0, 70, 85, …
## $ sore_throat              <dbl> 0, 0, 0, NA, 0, 0, 1, 0, 2, 1, 0, 3, 0, 0, 1,…
## $ view                     <dbl> 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, …

Exercise 1

Write the R code required to tally the total number of observations with thetally() function, and name the new variable “observations”.

laryngoscope %>% 
  ---(name = "---")
laryngoscope %>% 
  tally(name = "observations")

Exercise 2

If your data are currently grouped, tally() will count observations by group.

Write the R code required to (1) group_by() ASA class (“asa”), then (2) tally the total number of observations in each group with thetally() function, and name the new variable “obs”.

laryngoscope %>% 
  ---(asa) %>% 
  ---(name = ---)
laryngoscope %>% 
  group_by(asa) %>% 
  ---(name = ---)
laryngoscope %>% 
  group_by(asa) %>% 
  tally(name = "obs")

Exercise 3

You can do the grouping and counting in one function with count(), which will also ungroup after counting, so as not to change any previous grouping.

Write the R code required to (1) group_by() gender, then (2) count the total number of observations in each group with thecount() function.

laryngoscope %>% 
  count(---)
laryngoscope %>% 
  count(gender)

Exercise 4

Write the R code required to count the total number of observations in each group of asa and gender with thecount() function, then sort them in descending order, and name the count variable “n_per_group”.

laryngoscope %>% 
  count(---, ---, sort = ___, name = ___)
laryngoscope %>% 
  count(asa, gender, sort = ___, name = ___)
laryngoscope %>% 
  count(asa, gender, sort = TRUE, name = "n_per_group")

Exercise 5

Write the R code required to (1) add a “total_obs” column with the total number of observations in the study, and (2) add a “group_count” variable for each each combination of asa and gender, then (3) select asa, gender, group_count, and total_obs.

laryngoscope %>% 
  add_tally(name = "---") %>% 
  add_count(--, --, -- = "---") %>% 
  select(---)
laryngoscope %>% 
  add_tally(name = "---") %>% 
  add_count(asa, gender, name = "---") %>% 
  select(asa, gender, group_count, total_obs)
laryngoscope %>% 
  add_tally(name = "total_obs") %>% 
  add_count(asa, gender, name = "group_count") %>% 
  select(asa, gender, group_count, total_obs)

Exercise 6

At times you just want a sense of how many values are in a particular character or factor variable. This is where the janitor::tabyl() function shines. Write the R code required to count the number of distinct categories in the covid dataset variable, payor_group.

covid %>% 
  janitor::tabyl(---)
covid %>% 
  tabyl(---))
covid %>% 
  tabyl(payor_group)

Exercise 7

Write the R code required to count the number of observations by group in an R x C table in the covid dataset using the variables payor_group and gender.

covid %>% 
  ---(---, ---)
covid %>% 
  tabyl(---, ---)
covid %>% 
  tabyl(payor_group, gender)

Counting and Tallying Variables