BIOL2001 Chap.3 Indexing and Subsetting Vectors, Lists and Matrices
Indexing and Subsetting Vectors, Lists and Matrices
Picking the parts you need
Subsetting is how R code reaches into a structure and takes out the elements a task needs. This chapter compares the five kinds of index that work inside square brackets: positive numbers keep positions, negative numbers drop them, logical vectors keep the TRUE positions, an empty index keeps everything, and names keep named elements.
Positive and negative numbers cannot be mixed in one index.
Logical indexing and positions
A comparison inside the brackets works in two steps: R first evaluates it into a logical vector, then uses that vector to select. The function which() returns the positions of the TRUE elements instead of their values.
A figure traces both on a vector of water temperatures.
Matrices and lists
Matrices take a row index and a column index, with an empty side meaning all rows or all columns. Lists follow their own rules: the single bracket returns a list, the double bracket returns one element, and the dollar sign returns a named element.
What this chapter covers
- 01
Positive indexes keep positions
- 02
Negative indexes drop positions
- 03
Logical indexes and vectorised comparisons
- 04
Positions of TRUE elements with which()
- 05
Row and column indexes for matrices
- 06
Single brackets, double brackets and the dollar sign
Worked example · free
Selecting the warm readings from a temperature vector
- 1Compare each element with 20: FALSE, TRUE, FALSE, TRUE, TRUE, FALSE.
- 1Use the logical vector as the index to keep the TRUE values: 21.5, 23.4 and 20.1.
- 1which() returns the positions of the TRUE elements: 2, 4 and 5.
- 1sum(t > 20) counts TRUE as 1 and FALSE as 0, giving 3 readings.
Key terms
- Subsetting
- Selecting part of a data structure with an indexing operator such as square brackets.
- Negative Index
- An index that drops the listed positions and returns every other element.
- Logical Index
- A vector of TRUE and FALSE values that keeps the elements in the TRUE positions.
- Double Bracket
- The operator [[ ]] that extracts a single element from a list rather than a smaller list.
Indexing and Subsetting Vectors, Lists and Matrices FAQ
Can I mix positive and negative numbers in one R index?
No. R does not allow positive and negative numbers in the same index and stops with an error. Decide whether you are listing the positions to keep, with positive numbers, or the positions to drop, with negative numbers.
What is the difference between l[2] and l[[2]] for a list?
l[2] returns a list that contains the second element, because the single bracket never simplifies its result. l[[2]] returns the element itself. The double bracket accepts only a single number or name, so it cannot extract several elements at once.
How do I get the positions of elements that meet a condition?
Use which() around the condition. For a vector of six temperatures, which(t > 20) returns the positions 2, 4 and 5, which you can then use to index another vector of the same length, such as the matching dates.
Why does indexing one column of a matrix return a vector?
An empty row index with a single column number returns every row of that column, and R simplifies the result to a vector. Selecting two or more columns keeps the result as a matrix, because there are still two dimensions.
Assessment move
Make a six-element vector and a small matrix of your own, then write every kind of index from the comparison table against them and predict the output before running it. Say each index out loud in words, such as all rows except the first, columns one and two. Finish with a named list and practise reaching the same element with double brackets and with the dollar sign.
Finally, combine two ideas in one line, such as a logical condition on one column used to select rows of another, because that pattern carries straight into the data frame work of the next chapter.
Working through Indexing and Subsetting Vectors, Lists and Matrices in BIOL2001? Sia is AskSia’s AI Statistics tutor — ask any BIOL2001 Indexing and Subsetting Vectors, Lists and Matrices question and get a clear, step-by-step explanation grounded in how BIOL2001 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.
STAT1008 Quantitative Research Methods · BIOL1030 Diversity of Life · STAT7038 Regression Modelling