How do attributes and the class() of an object work in R?

Quick Overview

Explain how attributes attach metadata to R objects, what class() returns with and without an explicit class attribute, and how the class drives S3 method dispatch. Tests working knowledge of R objects, including the difference between class() and typeof().

How do attributes and the class() of an object work in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do attributes and the `class()` of an object work in R? Explain what attributes are, what `class()` returns, and how the class affects which function R runs on the object. ```hint Compare a few objects Call `attributes()`, `class()` and `typeof()` on a plain vector, a matrix, a factor and a data frame, and look at where the answers differ. ``` ### What a Strong Answer Covers - What attributes are and how to read and set them with `attr()`, `attributes()` or `structure()`. - That some attributes change how an object behaves, with `dim` turning a vector into a matrix as the standard example. - What `class()` returns when an explicit `class` attribute is set and when it is not (the implicit class). - How the `class` attribute drives S3 method dispatch, for example `print(x)` calling `print.myclass()`. - The difference between `class()` and `typeof()`, with an example where they disagree. ### Follow-up Questions - What does `inherits()` do, and why is it safer than `class(x) == "foo"`? - How does `NextMethod()` behave when an object's class vector has two or more entries? - Which common operations drop attributes, and how would you write a function that keeps them? - How do S4 classes differ from S3 in how a class is defined and checked?

Overview: Explain how attributes attach metadata to R objects, what class() returns with and without an explicit class attribute, and how the class drives S3 method dispatch. Tests working knowledge of R objects, including the difference between class() and typeof().

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

How do attributes and the class() of an object work in R? Explain what attributes are, what class() returns, and how the class affects which function R runs on the object.

What a Strong Answer Covers Guidance

  • What attributes are and how to read and set them with attr() , attributes() or structure() .
  • That some attributes change how an object behaves, with dim turning a vector into a matrix as the standard example.
  • What class() returns when an explicit class attribute is set and when it is not (the implicit class).
  • How the class attribute drives S3 method dispatch, for example print(x) calling print.myclass() .
  • The difference between class() and typeof() , with an example where they disagree.

Follow-up Questions Guidance

  • What does inherits() do, and why is it safer than class(x) == "foo" ?
  • How does NextMethod() behave when an object's class vector has two or more entries?
  • Which common operations drop attributes, and how would you write a function that keeps them?
  • How do S4 classes differ from S3 in how a class is defined and checked?