Explain the difference between a matrix and a data frame in R. When would you choose one over the other?

Quick Overview

Asks how an R matrix differs from a data frame and when each one is the right container. It tests knowledge of R storage and type coercion, matrix algebra versus mixed-type tabular data, and the conversion pitfalls that show up with as.matrix and apply.

Explain the difference between a matrix and a data frame in R. When would you choose one over the other?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the difference between a matrix and a data frame in R. When would you choose one over the other? ```hint Look underneath Run `typeof()` and `is.list()` on a small example of each and compare what they report. Then try putting a single string into one cell. ``` ### What a Strong Answer Covers - How each structure is stored underneath - The type rules, including what happens when types are mixed or a data frame is passed through `as.matrix()` - What kinds of work each one supports well - Concrete cases for choosing each, and how to convert between them ### Follow-up Questions - What does `as.matrix()` return for a data frame with one numeric and one character column, and how can that break a later calculation? - How would you turn a data frame with factor columns into a numeric design matrix for a regression? - Why does `apply(df, 1, f)` on a mixed-type data frame sometimes give surprising results?

Overview: Asks how an R matrix differs from a data frame and when each one is the right container. It tests knowledge of R storage and type coercion, matrix algebra versus mixed-type tabular data, and the conversion pitfalls that show up with as.matrix and apply.

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

Explain the difference between a matrix and a data frame in R. When would you choose one over the other?

What a Strong Answer Covers Guidance

  • How each structure is stored underneath
  • The type rules, including what happens when types are mixed or a data frame is passed through as.matrix()
  • What kinds of work each one supports well
  • Concrete cases for choosing each, and how to convert between them

Follow-up Questions Guidance

  • What does as.matrix() return for a data frame with one numeric and one character column, and how can that break a later calculation?
  • How would you turn a data frame with factor columns into a numeric design matrix for a regression?
  • Why does apply(df, 1, f) on a mixed-type data frame sometimes give surprising results?