Explain the concept of factor levels in R

Quick Overview

Asks what factor levels are in R and how they relate to the values a factor stores. Tests whether you understand how level order and unused levels affect sorting, frequency tables, plots and the baseline category in regression models.

Explain the concept of factor levels in R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the concept of factor levels in R. Factors are R's type for categorical data, and you meet them in data frames, frequency tables, plots and model formulas. ```hint What is stored Think about what a factor actually keeps for each observation, and what happens when a value does not match any level. ``` ### What a Strong Answer Covers - What a factor stores per observation and how that relates to its levels - How the default level order is chosen and how to set it explicitly - Where level order has visible effects: sorting, plots and the baseline category in models - What happens to values outside the level set and to levels no observation uses - The functions for reading, renaming, reordering and dropping levels, and a common conversion pitfall ### Follow-up Questions - A factor has labels "10", "5" and "20". Why does `as.numeric()` return the wrong numbers, and how do you get the real values? - What changes when a factor is ordered, both for comparisons and for the contrasts a linear model uses? - You fit a model on a subset where one level never appears, then predict on data that contains it. What happens, and why? - How would you collapse several rare levels into a single "Other" level?

Overview: Asks what factor levels are in R and how they relate to the values a factor stores. Tests whether you understand how level order and unused levels affect sorting, frequency tables, plots and the baseline category in regression models.

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

Explain the concept of factor levels in R.

Factors are R's type for categorical data, and you meet them in data frames, frequency tables, plots and model formulas.

What a Strong Answer Covers Guidance

  • What a factor stores per observation and how that relates to its levels
  • How the default level order is chosen and how to set it explicitly
  • Where level order has visible effects: sorting, plots and the baseline category in models
  • What happens to values outside the level set and to levels no observation uses
  • The functions for reading, renaming, reordering and dropping levels, and a common conversion pitfall

Follow-up Questions Guidance

  • A factor has labels "10", "5" and "20". Why does as.numeric() return the wrong numbers, and how do you get the real values?
  • What changes when a factor is ordered, both for comparisons and for the contrasts a linear model uses?
  • You fit a model on a subset where one level never appears, then predict on data that contains it. What happens, and why?
  • How would you collapse several rare levels into a single "Other" level?