Frequency Tables

A frequency table counts how many times each distinct value appears in your data, and in R you build them with table() and convert them to proportions with prop.table().

Learn Frequency Tables in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll learn one-way and two-way (cross) tabulation, turning counts into proportions and percentages, adding row and column totals with addmargins(), and binning continuous data with cut() before you count it.

What You'll Learn in This Lesson

1️⃣ One-Way Tables with table()

table() takes a vector and counts each unique value, returning a named table sorted alphabetically. It's the fastest way to see the distribution of a categorical variable.

2️⃣ Proportions with prop.table()

Counts answer "how many?"; proportions answer "what share?". Wrap a count table in prop.table() to get fractions that sum to 1, then multiply by 100 and round for friendly percentages.

3️⃣ Two-Way Tables & Margins

Pass two vectors to table() to cross-tabulate them — the first becomes rows, the second columns. addmargins() appends row and column totals (labelled Sum ), and margin.table() collapses to one variable's totals.

4️⃣ Binning with cut()

Continuous numbers (ages, prices) have too many unique values to tabulate directly. cut() slices them into labelled intervals — a factor — which you then feed to table() . You choose the boundaries with breaks and the names with labels .

Your turn. Fill in the ___ blanks, run it, and compare with the expected output.

Build the full picture from one vector pair: counts, a cross-tab, row proportions, and totals. This is exactly the workflow for summarising a categorical survey.

📋 Quick Reference — Frequency Tables

Practice quiz

What does table(x) do for a vector x?

  • Sorts the values
  • Removes duplicates
  • Counts how many times each unique value appears
  • Returns the mean

Answer: Counts how many times each unique value appears. table() counts the frequency of each distinct value.

How does table() order its category labels by default?

  • Alphabetically (or by factor level)
  • By descending count
  • By first appearance
  • Randomly

Answer: Alphabetically (or by factor level). table() sorts categories alphabetically, or by factor level order if set.

What does prop.table(table(x)) return?

  • Counts again
  • Proportions that sum to 1
  • Percentages out of 100
  • Raw frequencies

Answer: Proportions that sum to 1. prop.table() converts counts into proportions that sum to 1.

To get percentages from a count table, you would...

  • Use table(x, 100)
  • Use sum(table(x))
  • Divide by length only
  • Multiply prop.table(table(x)) by 100

Answer: Multiply prop.table(table(x)) by 100. prop.table() gives proportions; multiply by 100 for percentages.

In prop.table(tab, margin = 1), what sums to 1?

  • The grand total
  • Each column
  • Each row
  • Nothing

Answer: Each row. margin = 1 operates over rows, so each row's proportions sum to 1.

What does prop.table(tab, margin = 2) make sum to 1?

  • Each row
  • Each column
  • The whole table
  • The diagonal

Answer: Each column. margin = 2 operates over columns, so each column sums to 1.

What does addmargins(tab) do to a two-way table?

  • Appends row and column totals
  • Removes empty cells
  • Converts to proportions
  • Sorts by frequency

Answer: Appends row and column totals. addmargins() adds row/column totals labelled Sum.

Why bin numeric data with cut() before table()?

  • table() cannot accept numbers
  • cut() sorts the data
  • It removes outliers
  • Raw numbers have too many unique values to tabulate usefully

Answer: Raw numbers have too many unique values to tabulate usefully. cut() groups continuous values into a few labelled intervals (a factor).

By default, how does table() treat NA values?

  • Counts them as a category
  • Drops them unless useNA is set
  • Errors
  • Converts them to 0

Answer: Drops them unless useNA is set. NAs are dropped by default; add useNA = "ifany" to include them.

What does table(x, y) produce?

  • A two-way cross-tabulation of x and y
  • The sum of x and y
  • A merged vector
  • An error

Answer: A two-way cross-tabulation of x and y. Passing two vectors cross-tabulates them: first is rows, second is columns.