Vector Indexing
in R
Learn to extract and manipulate data from R vectors using numeric, logical, and name-based indexing techniques.
What Is a Vector?
A vector is the most fundamental data structure in R. It is an ordered collection of elements that all share the same data type: numeric, character, or logical.
Why Indexing Matters
Indexing lets you access, modify, and filter specific elements within a vector. Nearly every data analysis task in R relies on indexing to extract the information you need.
Three Ways to Index a Vector
R provides three powerful indexing strategies. Each uses the square bracket operator \( [\;] \) but with a different type of index inside.
Numeric Indexing: The Core Rule
Place an integer (or a vector of integers) inside the brackets to select elements by their position. Negative integers exclude elements instead.
Logical Indexing: Filter by Condition
Pass a logical vector of the same length as your data vector. Elements where the condition is TRUE are kept; elements where it is FALSE are dropped.
Name-Based Indexing
If your vector has named elements, you can select them using a character string inside the brackets. This makes code more readable and less prone to breakage if the order changes.
Key Distinction: Single vs. Multiple
The bracket operator \( [\;] \) always returns a vector, even when selecting a single element. Compare with other languages where indexing returns a scalar.
| Single Element | Multiple Elements | |
|---|---|---|
| Syntax | \( x[3] \) | \( x[c(1, 3, 5)] \) |
| Returns | Vector of length 1 | Vector of length \( n \) |
| Negative | \( x[-3] \) drops 3rd | \( x[-c(1,3)] \) drops 1st & 3rd |
| Logical | Not typical | \( x[x > 5] \) |
| Named | \( x[\text{"a"}] \) | \( x[c(\text{"a"},\text{"c"})] \) |
Range Indexing with the Colon Operator
The colon operator \( a:b \) generates a sequence of integers from \( a \) to \( b \). This is the most common way to select a contiguous slice of a vector.
How to Apply Vector Indexing
Example 1: Numeric Indexing
Given a vector of daily temperatures, extract the temperature on Wednesday (day 3) and the weekend (days 6 and 7).
Example 2: Logical Filtering
From a vector of exam scores, find all scores that are above 80.
Example 3: Combining Techniques
Replace all negative values in a vector with 0 using logical indexing and assignment.
The which() Function
\( \text{which()} \) converts a logical vector into numeric indices: it tells you where the TRUE values are, not what they are.
Common Mistakes
Indexing Summary
| Syntax | Use Case | Example | |
|---|---|---|---|
| Positive Numeric | \( x[i] \) | Select by position | \( x[3] \) |
| Negative Numeric | \( x[-i] \) | Exclude by position | \( x[-2] \) |
| Range | \( x[a:b] \) | Contiguous slice | \( x[2:5] \) |
| Logical | \( x[\text{cond}] \) | Filter by condition | \( x[x > 5] \) |
| Name-Based | \( x[\text{"name"}] \) | Select by label | \( x[\text{"age"}] \) |
Question 1 of 5
Given \( x \leftarrow c(5, 12, 8, 3, 20) \), what does \( x[4] \) return?
Question 2 of 5
Given \( y \leftarrow c(10, 20, 30, 40, 50) \), what does \( y[-c(2, 4)] \) return?
Question 3 of 5
Given \( z \leftarrow c(7, 2, 9, 4, 6) \), what does \( z[z \geq 6] \) return?
Question 4 of 5
Given \( \text{info} \leftarrow c(\text{age} = 17, \text{grade} = 11, \text{gpa} = 3.8) \), which correctly returns the GPA?
Question 5 of 5
What happens when you run \( x[0] \) in R where \( x \leftarrow c(4, 8, 12) \)?