← Back to R Course | Chapter 8: Data Manipulation | Lesson 6 of 7

table()

table counts how many times each value shows up, like tallying votes in a class election. It gives a neat count for each different choice.

In this page:

  1. table()
Syntax
r
table(x)
table(x, y)

table()

table(x) counts how many times each distinct value appears in a vector, producing a frequency table. It's the quickest way to summarize categorical data. Passing two vectors, table(x, y), produces a cross-tabulation counting combinations of both.

Note: table() is the fastest way to check the distribution of values in a categorical column before deeper analysis.

Example: table()

markup
colors <- c("red", "blue", "red", "green", "blue", "red")

print(table(colors))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting table() to return a plain vector, when it returns a table object with names.
  2. Counting a numeric column with many unique values, which produces a very long table.
  3. Missing NA values because they are excluded by default; use useNA = "ifany".
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.