Which data structure is used to store categorical variables in R?

Quick Overview

Asks which R data structure is built for categorical variables and how it represents the categories internally. Tests knowledge of ordered and unordered categories, how models and plots treat them, and the conversion pitfalls that trip up R users.

Which data structure is used to store categorical variables in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Which data structure is used to store categorical variables in R? A categorical variable takes one of a fixed set of values, such as a customer's region or a survey answer on a small-medium-large scale. ```hint Think about repeated values Picture a column with a million rows but only five distinct strings. How could R store it compactly and still know which values are allowed? ``` ### What a Strong Answer Covers - Names the structure and the function that creates it. - Explains how the values are represented internally. - Distinguishes categories with no order from categories with a natural rank. - Says why this structure is preferred over a plain character vector for modeling and plotting. - Mentions at least one common pitfall when converting or subsetting these values. ### Follow-up Questions - What does `as.numeric()` return for a categorical column whose labels are numbers like "10" and "5", and how do you recover the original values? - After subsetting so that some categories no longer appear, what happens to the category set, and how do you clean it up? - How does `lm()` turn a categorical predictor with k categories into model columns, and how do you change the reference category? - What changed about `stringsAsFactors` in R 4.0.0, and why does it matter for older code?

Overview: Asks which R data structure is built for categorical variables and how it represents the categories internally. Tests knowledge of ordered and unordered categories, how models and plots treat them, and the conversion pitfalls that trip up R users.

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

Which data structure is used to store categorical variables in R?

A categorical variable takes one of a fixed set of values, such as a customer's region or a survey answer on a small-medium-large scale.

What a Strong Answer Covers Guidance

  • Names the structure and the function that creates it.
  • Explains how the values are represented internally.
  • Distinguishes categories with no order from categories with a natural rank.
  • Says why this structure is preferred over a plain character vector for modeling and plotting.
  • Mentions at least one common pitfall when converting or subsetting these values.

Follow-up Questions Guidance

  • What does as.numeric() return for a categorical column whose labels are numbers like "10" and "5", and how do you recover the original values?
  • After subsetting so that some categories no longer appear, what happens to the category set, and how do you clean it up?
  • How does lm() turn a categorical predictor with k categories into model columns, and how do you change the reference category?
  • What changed about stringsAsFactors in R 4.0.0, and why does it matter for older code?