Subsetting
Data Frames
Master the art of selecting rows and columns from data frames using R's powerful bracket notation and the dollar sign operator.
What Is a Data Frame?
A data frame is R's primary structure for storing tabular data: a rectangular collection of values organized into rows (observations) and columns (variables). Think of it like a spreadsheet inside your code.
Why Subset a Data Frame?
Real data sets contain far more information than you need for any single analysis. Subsetting lets you focus on exactly the data that matters.
The Two Core Operators
R gives you two fundamental tools for subsetting data frames. Every technique you learn builds on these.
Bracket Notation Explained
The $ Dollar Sign Operator
The $ operator extracts a single column from a data frame and returns it as a vector. It is the most common way to access one variable.
# Extract the "score" column as a vector df$score # [1] 88 92 75 81 # Use it in calculations mean(df$score) # [1] 84 # Use it for logical conditions df$score > 80 # [1] TRUE TRUE FALSE TRUE
[ , ] vs $: Key Differences
| \(\text{df}[ , ]\) | \(\text{df}\$\text{col}\) | |
|---|---|---|
| Selects | Rows, columns, or both | One column only |
| Returns | Data frame or vector | Always a vector |
| By number? | ✓ Yes | ✕ No |
| By name? | ✓ Yes (quoted) | ✓ Yes (unquoted) |
| Multiple cols? | ✓ Yes | ✕ No |
| Row filtering? | ✓ Yes | ✕ No |
Four Ways to Subset with Brackets
How to Apply Subsetting
Example 1: Selecting Columns
# Our data frame df <- data.frame( name = c("Alice", "Bob", "Cara", "Dan"), age = c(23, 31, 27, 19), score = c(88, 92, 75, 81) ) # Method 1: by name using $ df$name # [1] "Alice" "Bob" "Cara" "Dan" # Method 2: by position using brackets df[, 2] # [1] 23 31 27 19 # Method 3: multiple columns by name df[, c("name", "score")]
Example 2: Filtering Rows
# Get row 1 df[1, ] # name age score # 1 Alice 23 88 # Get rows 2 through 4 df[2:4, ] # Logical filter: students older than 20 df[df$age > 20, ] # name age score # 1 Alice 23 88 # 2 Bob 31 92 # 3 Cara 27 75
Example 3: Rows + Columns Together
# Names of students who scored above 80 df[df$score > 80, "name"] # [1] "Alice" "Bob" "Dan" # Names and scores of those students df[df$score > 80, c("name", "score")] # name score # 1 Alice 88 # 2 Bob 92 # 4 Dan 81 # A single cell: row 2, column "name" df[2, "name"] # [1] "Bob"
Negative Indexing: Dropping Data
In R, a negative index means "everything except this." Use it to exclude specific rows or columns without listing everything you want to keep.
# Drop column 2 (age) df[, -2] # name score # 1 Alice 88 # 2 Bob 92 # 3 Cara 75 # 4 Dan 81 # Drop rows 1 and 3 df[-c(1, 3), ] # name age score # 2 Bob 31 92 # 4 Dan 19 81
Logical Subsetting Deep Dive
Logical subsetting is the most powerful technique. Build conditions using comparison operators, then pass the result to the row position.
# Combine conditions with & (AND) df[df$age > 20 & df$score > 80, ] # name age score # 1 Alice 23 88 # 2 Bob 31 92 # Use | (OR) for either condition df[df$age < 21 | df$score > 90, ] # name age score # 2 Bob 31 92 # 4 Dan 19 81
Common Mistakes to Avoid
Summary Reference Table
| Goal | Syntax | Returns |
|---|---|---|
| One column (vector) | \(\text{df}\$\text{col}\) | Vector |
| One column (data frame) | \(\text{df}[, \text{"col"}, \text{drop}=\text{FALSE}]\) | Data frame |
| Multiple columns | \(\text{df}[, \text{c}(\text{"a"},\text{"b"})]\) | Data frame |
| One row | \(\text{df}[1, ]\) | Data frame |
| Filter rows | \(\text{df}[\text{df}\$x > 5, ]\) | Data frame |
| Single cell | \(\text{df}[2, \text{"col"}]\) | Scalar |
| Exclude column | \(\text{df}[, -2]\) | Data frame |
Question 1 of 5
Given a data frame df with columns name, age, and score, which expression returns the age column as a vector?
Question 2 of 5
What does \(\text{df}[3, ]\) return?
Question 3 of 5
Which expression correctly filters for all rows where \(\text{score} > 85\)?
Question 4 of 5
What does \(\text{df}[, -1]\) do?
Question 5 of 5
You need the names and scores of students whose \(\text{age} \geq 25\). Which expression accomplishes this?