How do you create and subset a data frame in R?

Quick Overview

Asks how to build a data frame in base R and select rows and columns from it. Tests bracket indexing, the difference between the $, [[ ]] and [ ] accessors, when a single-column selection turns into a vector, and how missing values affect row filters.

How do you create and subset a data frame in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you create and subset a data frame in R? ```hint Watch the return type For each way of pulling out a column (`$`, `[[ ]]`, `[ ]`, and `[ , ]`), ask whether you get back a vector or a data frame. ``` ### What a Strong Answer Covers - How to build a data frame and what R requires of its columns - The `df[rows, cols]` syntax: empty slots, positional, logical and name-based indexing - The difference between `$`, `[[ ]]` and single-bracket column access - The `drop` behavior when one column is selected, and how to keep a data frame - A pitfall when filtering rows, such as missing values in the condition ### Follow-up Questions - What happens when the column you filter on contains `NA`, and how do you avoid it? - How would you write the same row and column selection with dplyr or data.table? - How does `df[, 1]` behave on a tibble compared with a base data frame?

Overview: Asks how to build a data frame in base R and select rows and columns from it. Tests bracket indexing, the difference between the $, [[ ]] and [ ] accessors, when a single-column selection turns into a vector, and how missing values affect row filters.

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

How do you create and subset a data frame in R?

What a Strong Answer Covers Guidance

  • How to build a data frame and what R requires of its columns
  • The df[rows, cols] syntax: empty slots, positional, logical and name-based indexing
  • The difference between $ , [[ ]] and single-bracket column access
  • The drop behavior when one column is selected, and how to keep a data frame
  • A pitfall when filtering rows, such as missing values in the condition

Follow-up Questions Guidance

  • What happens when the column you filter on contains NA , and how do you avoid it?
  • How would you write the same row and column selection with dplyr or data.table?
  • How does df[, 1] behave on a tibble compared with a base data frame?