Subsetting & Indexing
Subsetting is the act of pulling specific elements out of an R object using square brackets, with [ ] keeping a subset, [[ ]] extracting one element, and $ reaching a named element of a list.
Learn Subsetting & Indexing in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end you'll confidently index by position, by negative position, by logical condition, and by name, and you'll know exactly when to use [ ] versus [[ ]] versus $ on vectors and lists.
What You'll Learn in This Lesson
1️⃣ Indexing a Vector: Four Ways
Single brackets accept four kinds of index. A positive number selects by position, a vector of numbers selects several, a negative number drops positions, and a name (if your vector is named) selects by label. R indexes from 1 .
2️⃣ Logical Indexing & which()
The most powerful form: pass a logical vector and keep the elements where it is TRUE . The comparison itself ( nums > 10 ) produces that logical vector. Use which() when you want the positions instead of the values, and combine tests with & (and) / | (or).
3️⃣ Lists: [ ] vs [[ ]] vs $
Lists hold elements of different types, so the bracket you choose matters. [ ] keeps the list wrapper and returns a shorter list. [[ ]] reaches inside and returns the bare element. $ is a friendly shorthand for [[ ]] by name. Once you have the element with [[ ]] or $ , you can index into it normally.
Memorise the one-liner: [ ] keeps the box, [[ ]] and $ open the box.
Your turn. Fill in the ___ blanks, run it, and compare with the expected output.
Mini-Challenge: Dig Into a List
This challenge forces you to switch between $ , [[ ]] , and [ ] on the same object — exactly the muscle memory you need for real data work.
Common Errors (and the fix)
- ❌ x[0] returns an empty result. ✅ R indexes from 1 — the first element is x[1] .
- ❌ Doing math on person[1] fails (it's still a list). ✅ Use person[[1]] or person$name .
- ❌ Mixing signs: x[c(1, -2)] errors. ✅ Use all-positive or all-negative indices.
- ❌ Using && for element-wise filtering. ✅ Use single & / | inside brackets — && only compares the first element.
Pro Tips
- 💡 Assign into a subset: x[x < 0] <- 0 replaces every negative value with 0 in one line.
- 💡 %in% for membership: x[x %in% c(2, 4)] keeps values found in a set.
- 💡 head()/tail(): head(x, 3) grabs the first three without writing the indices.
📋 Quick Reference — Subsetting
Task
R Syntax
By position
x[2]
Drop a position
x[-1]
By condition
x[x > 10]
Positions that match
which(x > 10)
List element (value)
lst[[1]] / lst$name
List subset (wrapped)
lst[1]
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Index a vector by position , negative position , condition , or name
- ✅ x[x > 10] filters; which() returns the positions
- ✅ [ ] keeps the box , [[ ]] and $ open it
- ✅ Combine conditions with single & and |
- ✅ Next lesson: Missing Data — handling NA with is.na() and na.rm
Practice quiz
In R, what does x[1] return for a vector x?
- The first element (R indexes from 1)
- An empty result
- The second element
- The whole vector
Answer: The first element (R indexes from 1). R is 1-indexed, so x[1] is the first element.
What does x[-1] do?
- Returns the last element
- Returns everything except the first element
- Errors
- Returns NA
Answer: Returns everything except the first element. A negative index drops that position, so x[-1] is all but the first.
What does x[0] return in R?
- The first element
- An error
- An empty vector of the same type
- NULL
Answer: An empty vector of the same type. Index 0 selects nothing, returning a length-0 vector of x's type.
Which operator extracts a single VALUE from a list, dropping the wrapper?
[[ ]] reaches inside and returns the bare element; [ ] keeps the list wrapper.
What does lst$name do on a list?
- Returns a list of length 1
$ is a friendly shorthand for [[ ]] by name on lists and data frames.
Given nums, what does nums[nums > 10] return?
- The positions where nums > 10
- The count of elements over 10
- The elements whose value exceeds 10
- TRUE or FALSE
Answer: The elements whose value exceeds 10. A logical vector inside [ ] keeps the elements where the test is TRUE.
What does which(nums > 10) return?
- The integer positions that are TRUE
- The matching values
- A single TRUE/FALSE
- The number of matches
Answer: The integer positions that are TRUE. which() converts a logical vector into the positions where it is TRUE.
Why does person[1] give a list but person[[1]] give the value?
[ ] preserves the list wrapper; [[ ]] extracts the element itself.
Which correctly combines two element-wise conditions inside [ ]?
Single & is element-wise; && only compares the first element.
What happens with x[c(1, -2)] mixing positive and negative indices?
- Returns elements 1 and 2
- Drops element 2 only
- Returns the whole vector
- It is an error
Answer: It is an error. You cannot mix positive and negative indices in one bracket.
Continue this course
- Previous: Data Types & Classes
- Next: Missing Data (NA)