table()
table(..., exclude = NA, useNA = c("no", "ifany", "always")) Returns:
table · Updated March 13, 2026 · Base Functions statistics frequency base categorical
The table() function creates contingency tables, also known as frequency tables. It counts the occurrences of each unique value in one or more vectors or factors, making it essential for exploratory data analysis and categorical data summarization. The function is part of base R and requires no additional packages.
Syntax
table(..., exclude = NA, useNA = c("no", "ifany", "always"))
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | vectors/factors | required | One or more vectors or factors to cross-tabulate |
exclude | character/NA | NA | Values to exclude from the table; use NA to keep NA counts |
useNA | character | "no" | Controls NA handling: "no" (no NA column), "ifany" (include if present), "always" (always include) |
Examples
Basic one-way table
colors <- c("red", "blue", "red", "green", "blue", "red")
table(colors)
# colors
# blue green red
# 2 1 3
Two-way cross-tabulation
gender <- c("M", "F", "M", "F", "M", "F", "M", "M")
outcome <- c("pass", "pass", "fail", "pass", "pass", "fail", "pass", "fail")
table(gender, outcome)
# outcome
# gender fail pass
# F 1 2
# M 2 3
Using useNA
scores <- c(85, 92, NA, 78, NA, 88, 95)
table(scores, useNA = "ifany")
# scores
# 78 85 88 92 95 <NA>
# 1 1 1 1 1 2
Common Patterns
Proportions
colors <- c("red", "blue", "red", "green", "blue", "red", "green", "green")
tbl <- table(colors)
prop.table(tbl)
# colors
# blue green red
# 0.25 0.375 0.375
Margin tables
gender <- c("M", "F", "M", "F", "M", "F")
outcome <- c("pass", "pass", "fail", "pass", "pass", "fail")
addmargins(table(gender, outcome))
# outcome
# gender fail pass Sum
# F 1 2 3
# M 2 2 4
# Sum 3 4 7