What are factors in R and how do levels and ordered factors work?

Quick Overview

Explain what factors are in R and how levels and ordered factors behave. Tests knowledge of how R stores categorical data, how level order affects plots, tables and model baselines, what ordered factors change in models, and the common conversion traps.

What are factors in R and how do levels and ordered factors work?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What are factors in R and how do levels and ordered factors work? ```hint Look underneath Run `unclass()` or `str()` on a factor to see how it is stored, then think about what decides the order of its labels and where that order shows up. ``` ### What a Strong Answer Covers - How a factor is stored and what the levels attribute holds - How the level order is set by default, how to change it, and where it affects plots, tables and models - What an ordered factor adds, including comparisons and its default treatment in models - At least one common conversion or subsetting trap and how to fix it ### Follow-up Questions - How do you change the reference level of a factor used in `lm()`, and what happens to the coefficients? - After filtering a data frame, a factor still lists levels that have no rows. Why, and how do you fix it? - When would you model an ordinal variable as an ordered factor, and when as a plain number?

Overview: Explain what factors are in R and how levels and ordered factors behave. Tests knowledge of how R stores categorical data, how level order affects plots, tables and model baselines, what ordered factors change in models, and the common conversion traps.

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

What are factors in R and how do levels and ordered factors work?

What a Strong Answer Covers Guidance

  • How a factor is stored and what the levels attribute holds
  • How the level order is set by default, how to change it, and where it affects plots, tables and models
  • What an ordered factor adds, including comparisons and its default treatment in models
  • At least one common conversion or subsetting trap and how to fix it

Follow-up Questions Guidance

  • How do you change the reference level of a factor used in lm() , and what happens to the coefficients?
  • After filtering a data frame, a factor still lists levels that have no rows. Why, and how do you fix it?
  • When would you model an ordinal variable as an ordered factor, and when as a plain number?