Australian National University · FACULTY OF STATISTICS

BIOL2001 Chap.3 Indexing and Subsetting Vectors, Lists and Matrices

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 3 of 15 · BIOL2001

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.

In this chapter

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

Q [4 marks]. For t <- c(18.2, 21.5, 19.0, 23.4, 20.1, 17.6), give the logical vector t > 20, the values t[t > 20], the positions which(t > 20) and the number of readings above 20. The practice weighting is assigned by AskSia and is not an official assessment.
The six temperatures with the logical vector t > 20 beneath them: the three TRUE positions pass their values into t[t > 20], while which() returns the positionsThe six temperatures with the logical vector t > 20 beneath them: the three TRUE positions pass their values into t[t > 20], while which() returns the positions 2, 4 and 5 rather than the values.tt > 20[1]18.2FALSE[2]21.5TRUE[3]19.0FALSE[4]23.4TRUE[5]20.1TRUE[6]17.6FALSEt[t > 20]: 21.5 23.4 20.1which(t > 20): 2 4 5
The six temperatures with the logical vector t > 20 beneath them: the three TRUE positions pass their values into t[t > 20], while which() returns the positions 2, 4 and 5 rather than the values.
  • 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.
The comparison gives FALSE TRUE FALSE TRUE TRUE FALSE, the selected values are 21.5, 23.4 and 20.1 at positions 2, 4 and 5, and three readings exceed 20 degrees.
Sia tip — Decide whether the question wants values or positions before you type: t[t > 20] returns temperatures, while which(t > 20) returns their places in the vector.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 46 of your Australian National University subjects - and 1,000+ Bibles across every Australian university.
Sia - your BIOL2001 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works