What is the difference between the single bracket [, double bracket [[, and $ operators for subsetting in R?

Quick Overview

Asks how the single bracket, double bracket and dollar operators differ when subsetting lists, vectors and data frames in R. Tests your understanding of what each operator returns, how names are matched, and how dropped dimensions and missing names cause subtle bugs.

What is the difference between the single bracket [, double bracket [[, and $ operators for subsetting in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the difference between the single bracket `[`, double bracket `[[`, and `$` operators for subsetting in R? ```hint Inspect the result Try each operator on a small named list and on a data frame, and look at `class()` and `length()` of what comes back. ``` ### What a Strong Answer Covers - What each operator returns on a list, and how that relates to the type of the original object. - How many elements each operator can select and which kinds of index each accepts. - How the operators behave on a data frame, including when `[` drops dimensions. - Differences in name matching, including partial matching and using a variable that holds a name. - What each operator returns or raises for a missing name or an out-of-range index. ### Follow-up Questions - What does `l["a"] <- NULL` do compared with `l["a"] <- list(NULL)`? - Why can `df[, "x"]` break code that worked with `df[, c("x", "y")]`, and how do you guard against it? - How do tibbles change the behavior of `[` and `$` compared with base data frames?

Overview: Asks how the single bracket, double bracket and dollar operators differ when subsetting lists, vectors and data frames in R. Tests your understanding of what each operator returns, how names are matched, and how dropped dimensions and missing names cause subtle bugs.

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

What is the difference between the single bracket [, double bracket [[, and $ operators for subsetting in R?

What a Strong Answer Covers Guidance

  • What each operator returns on a list, and how that relates to the type of the original object.
  • How many elements each operator can select and which kinds of index each accepts.
  • How the operators behave on a data frame, including when [ drops dimensions.
  • Differences in name matching, including partial matching and using a variable that holds a name.
  • What each operator returns or raises for a missing name or an out-of-range index.

Follow-up Questions Guidance

  • What does l["a"] <- NULL do compared with l["a"] <- list(NULL) ?
  • Why can df[, "x"] break code that worked with df[, c("x", "y")] , and how do you guard against it?
  • How do tibbles change the behavior of [ and $ compared with base data frames?