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:
Syntax
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()
colors <- c("red", "blue", "red", "green", "blue", "red")
print(table(colors))
Login to try C/C++/Java/PHP code in the editor
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
- Expecting
table()to return a plain vector, when it returns a table object with names. - Counting a numeric column with many unique values, which produces a very long table.
- Missing
NAvalues because they are excluded by default; useuseNA = "ifany".
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: