How to Create Frequency Tables in R with table() and dplyr
A frequency table shows how many times each value appears in a dataset. When you create frequency tables in R, you can choose between base R’s table() for simple counts and dplyr::count() for tidy data frame output, depending on whether you need raw summary output or something that integrates with downstream analysis steps.
# Base R: table() returns a named integer vector
colors <- c("red", "blue", "red", "green", "blue", "red")
table(colors)
# blue green red
# 2 1 3
# Include NAs
table(scores, useNA = "ifany")
library(dplyr)
# dplyr: count() returns a tibble, sortable with sort = TRUE
df <- data.frame(color = c("red", "blue", "red", "green", "blue", "red"))
df |> count(color, sort = TRUE)
# # A tibble: 3 × 2
# color n
# 1 red 3
# 2 blue 2
# 3 green 1
Convert counts to proportions with prop.table(table(colors)) in base R, or with count(df, color) |> mutate(prop = n / sum(n)) in dplyr. For two-way frequency tables, table(x, y) produces a matrix; count(df, x, y) produces a long-format data frame that works better with ggplot2. When you need cumulative frequencies alongside raw counts, cumsum(table(x)) gives running totals in one line. For tables with many levels, pipe through arrange(desc(n)) to see the most frequent categories first, and use slice_max(n, n = 5) to limit output to the top entries. The janitor::tabyl() function produces frequency tables with percentages and valid-percent columns in a single call.
See also
- table(), Base R contingency tables
- dplyr::count(), Tidyverse counting
- how-to-count-rows-by-group, Related counting patterns