Frequency Counts
Last updated: January 18, 2021
Frequency counts for a single categorical variable
df %>% count(smoke)
# A tibble: 2 x 2
# smoke n
# <int> <int>
# 1 0 115
# 2 1 74
Frequency counts for combinations of variables
df %>%
group_by(smoke, low) %>%
summarize(n = n())
# A tibble: 4 x 3
# Groups: smoke [2]
# smoke low n
# <int> <int> <int>
# 1 0 0 86
# 2 0 1 29
# 3 1 0 44
# 4 1 1 30
Setup for running the example code
This example uses the birthwt
dataset. If you want to run the example code, first run this:
library(MASS) # Needed for `birthwt` dataset
library(tidyverse)
df <- birthwt # Information: https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/birthwt.html
âšī¸ This page is part of my knowledge base for R, the popular statistical programming language. I attempt to use idiomatic practices with the tidyverse
collection of packages as much as possible. If you have suggestions for ways to improve this code, please contact me or use the survey link below..