What does the table() function do and how is it used for cross-tabulation?

Quick Overview

Asks what the R table() function returns and how to use it to cross-tabulate two or more categorical variables. Tests practical R skills: frequency counts, contingency tables, proportions by margin, totals, missing values and related tools such as xtabs().

What does the table() function do and how is it used for cross-tabulation?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What does the `table()` function do and how is it used for cross-tabulation? Answer for R, and show a small example on a built-in dataset such as `mtcars`. ```hint Think beyond the raw counts Consider what happens to missing values and to factor levels that never occur, and how you would get row percentages from the result. ``` ### What a Strong Answer Covers - What `table()` returns for one variable and for two or more variables - How missing values and unused factor levels are treated, and how to change that - Turning counts into proportions along a chosen margin, and adding totals - Related tools such as `xtabs()`, `ftable()` and conversion to a data frame - A small worked example with correct output ### Follow-up Questions - How would you get the share of each gear count within each cylinder group? - How do you test whether the two variables in a cross-tabulation are independent? - How would you cross-tabulate a continuous variable such as `mpg` against `cyl`? - When would you use `xtabs()` with a left-hand side instead of `table()`?

Overview: Asks what the R table() function returns and how to use it to cross-tabulate two or more categorical variables. Tests practical R skills: frequency counts, contingency tables, proportions by margin, totals, missing values and related tools such as xtabs().

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What does the table() function do and how is it used for cross-tabulation?

Answer for R, and show a small example on a built-in dataset such as mtcars.

What a Strong Answer Covers Guidance

  • What table() returns for one variable and for two or more variables
  • How missing values and unused factor levels are treated, and how to change that
  • Turning counts into proportions along a chosen margin, and adding totals
  • Related tools such as xtabs() , ftable() and conversion to a data frame
  • A small worked example with correct output

Follow-up Questions Guidance

  • How would you get the share of each gear count within each cylinder group?
  • How do you test whether the two variables in a cross-tabulation are independent?
  • How would you cross-tabulate a continuous variable such as mpg against cyl ?
  • When would you use xtabs() with a left-hand side instead of table() ?