R’s subsetting operators are powerful and fast. Mastery of subsetting allows you to succinctly express complex operations in a way that few other languages can match. Subsetting is easy to learn but hard to master because you need to internalise a number of interrelated concepts:
The six types of thing that you can subset with.
The three subsetting operators,
How the subsetting operators interact with vector types (e.g., atomic vectors, lists, factors, matrices, and data frames).
The use of subsetting together with assignment.
This chapter helps you master subsetting by starting with the simplest type of subsetting: subsetting an atomic vector with
[. It then gradually extends your knowledge, first to more complicated data types (like arrays and lists), and then to the other subsetting operators,
$. You’ll then learn how subsetting and assignment can be combined to modify parts of an object, and, finally, you’ll see a large number of useful applications.
Subsetting is a natural complement to
str() shows you the structure of any object, and subsetting allows you to pull out the pieces that you’re interested in. For large, complex objects, I also highly recommend the interactive RStudio Viewer, which you can activate with
Take this short quiz to determine if you need to read this chapter. If the answers quickly come to mind, you can comfortably skip this chapter. Check your answers in Section 5.6.
What is the result of subsetting a vector with positive integers, negative integers, a logical vector, or a character vector?
What’s the difference between
$when applied to a list?
When should you use
drop = FALSE?
xis a matrix, what does
x <- 0do? How is it different to
x <- 0?
How can you use a named vector to relabel categorical variables?
Section 5.2 starts by teaching you about
[. You’ll start by learning the six types of data that you can use to subset atomic vectors. You’ll then learn how those six data types act when used to subset lists, matrices, and data frames.
Section 5.3 expands your knowledge of subsetting operators to include
$, focussing on the important principles of simplifying vs. preserving.
In Section 5.4 you’ll learn the art of subassignment, combining subsetting and assignment to modify parts of an object.
Section 5.5 leads you through eight important, but not obvious, applications of subsetting to solve problems that you often encounter in a data analysis.
5.2 Selecting multiple elements
It’s easiest to learn how subsetting works for atomic vectors, and then how it generalises to higher dimensions and other more complicated objects. We’ll start with
[, the most commonly used operator which allows you to extract any number of elements. Section 5.3 will cover
$, used to extra a single element from a data structure.
5.2.1 Atomic vectors
Let’s explore the different types of subsetting with a simple vector,
Note that the number after the decimal point gives the original position in the vector.
There are six things that you can use to subset a vector:
Positive integers return elements at the specified positions:
Negative integers omit elements at the specified positions:
You can’t mix positive and negative integers in a single subset:
Logical vectors select elements where the corresponding logical value is
TRUE. This is probably the most useful type of subsetting because you can write an expression that creates the logical vector:
If the logical vector is shorter than the vector being subsetted, it will be silently recycled to be the same length.
A missing value in the index always yields a missing value in the output:
Nothing returns the original vector. This is not useful for 1d vectors, as you’ll see shortly, is very useful for matrices, data frames, and arrays. It can also be useful in conjunction with assignment.
Zero returns a zero-length vector. This is not something you usually do on purpose, but it can be helpful for generating test data.
If the vector is named, you can also use character vectors to return
elements with matching names.
(y <- setNames(x, letters[1:4])) #> a b c d #> 2.1 4.2 3.3 5.4 y[c("d", "c", "a")] #> d c a #> 5.4 3.3 2.1 # Like integer indices, you can repeat indices y[c("a", "a", "a")] #> a a a #> 2.1 2.1 2.1 # When subsetting with [, names are always matched exactly z <- c(abc = 1, def = 2) z[c("a", "d")] #> <NA> <NA> #> NA NA
Subsetting a list works in the same way as subsetting an atomic vector. Using
[ will always return a list;
$, as described in Section 5.3, let you pull out the components of the list.
5.2.3 Matrices and arrays
You can subset higher-dimensional structures in three ways:
- With multiple vectors.
- With a single vector.
- With a matrix.
The most common way of subsetting matrices (2d) and arrays (>2d) is a simple generalisation of 1d subsetting: you supply a 1d index for each dimension, separated by a comma. Blank subsetting is now useful because it lets you keep all rows or all columns.
[ will simplify the results to the lowest possible dimensionality. You’ll learn how to avoid this in Section 5.2.5.
Because matrices and arrays are just vectors with special attributes, you can subset them with a single vector, as if they were a 1d vector. Arrays in R are stored in column-major order:
vals <- outer(1:5, 1:5, FUN = "paste", sep = ",") vals #> [,1] [,2] [,3] [,4] [,5] #> [1,] "1,1" "1,2" "1,3" "1,4" "1,5" #> [2,] "2,1" "2,2" "2,3" "2,4" "2,5" #> [3,] "3,1" "3,2" "3,3" "3,4" "3,5" #> [4,] "4,1" "4,2" "4,3" "4,4" "4,5" #> [5,] "5,1" "5,2" "5,3" "5,4" "5,5" vals[c(4, 15)] #>  "4,1" "5,3"
You can also subset higher-dimensional data structures with an integer matrix (or, if named, a character matrix). Each row in the matrix specifies the location of one value, where each column corresponds to a dimension in the array being subsetted. This means that you use a 2 column matrix to subset a matrix, a 3 column matrix to subset a 3d array, and so on. The result is a vector of values:
5.2.4 Data frames and tibbles
Data frames possess the characteristics of both lists and matrices: if you subset with a single vector, they behave like lists; if you subset with two vectors, they behave like matrices.
df <- data.frame(x = 1:3, y = 3:1, z = letters[1:3]) df[df$x == 2, ] #> x y z #> 2 2 2 b df[c(1, 3), ] #> x y z #> 1 1 3 a #> 3 3 1 c # There are two ways to select columns from a data frame # Like a list, which df[c("x", "z")] #> x z #> 1 1 a #> 2 2 b #> 3 3 c # Like a matrix df[, c("x", "z")] #> x z #> 1 1 a #> 2 2 b #> 3 3 c # There's an important difference if you select a single # column: matrix subsetting simplifies by default, list # subsetting does not. str(df["x"]) #> 'data.frame': 3 obs. of 1 variable: #> $ x: int 1 2 3 str(df[, "x"]) #> int [1:3] 1 2 3
Subsetting a tibble with
[ always returns a tibble:
5.2.5 Preserving dimensionality
By default, any subsetting 2d data structures with a single number, single name, or a logical vector containing a single
TRUE will simplify the returned output, i.e. it will return an object with lower dimensionality. To preserve the original dimensionality, you must use
drop = FALSE
For matrices and arrays, any dimensions with length 1 will be dropped:
Data frames with a single column will return just that column:
Tibbles default to
drop = FALSE, and
[will never return a single vector.
drop = TRUE behaviour is a common source of bugs in functions: you check your code with a data frame or matrix with multiple columns, and it works. Six months later you (or someone else) uses it with a single column data frame and it fails with a mystifying error. When writing functions, get in the habit of always using
drop = FALSE when subsetting a 2d object.
Factor subsetting also has a
drop argument, but the meaning is rather different. It controls whether or not levels are preserved (not the dimensionality), and it defaults to
FALSE (levels are preserved, not simplified by default). If you find you are using
drop = TRUE a lot it’s often a sign that you should be using a character vector instead of a factor.
Fix each of the following common data frame subsetting errors:
Why does the following code yield five missing values? (Hint: why is it different from
upper.tri()return? How does subsetting a matrix with it work? Do we need any additional subsetting rules to describe its behaviour?
mtcars[1:20]return an error? How does it differ from the similar
Implement your own function that extracts the diagonal entries from a matrix (it should behave like
xis a matrix).
df[is.na(df)] <- 0do? How does it work?
5.3 Selecting a single element
There are two other subsetting operators:
[[ is used for extracting single items, and
x$y is a useful shorthand for
[[ is most important working with lists because subsetting a list with
[ always returns a smaller list. To help make this easier to understand we can use a metaphor:
xis a train carrying objects, then
x[]is the object in car 5;
x[4:6]is a train of cars 4-6.”
Let’s make a simple list and draw it as a train:
When extracting a single element, you have two options: you can create a smaller train, or you can extract the contents of a carriage. This is the difference between
When extracting multiple elements (or zero!), you have to make a smaller train:
Because it can return only a single item, you must use
[[ with either a single positive integer or a string. If you use a vector with
[[, it will subset recursively:
[[ is crucial for working with lists, but I recommend using it whenever you want your code to clearly express that it’s working with a single item. That frequently arises in for loops, e.g., instead of writing:
It’s better to write:
That reinforces to the reader that you expect to get and set individual values.
$ is a shorthand operator:
x$y is roughly equivalent to
x[["y"]]. It’s often used to access variables in a data frame, as in
diamonds$carat. One common mistake with
$ is to use it when you have the name of a column stored in a variable:
There’s one important difference between
$ does partial matching:
To help avoid this behaviour I highly recommend setting the global option
(For data frames, you can also avoid this problem by using tibbles instead: they never do partial matching.)
5.3.3 Missing/out of bounds indices
It’s useful to understand what happens with
[[ when you use an “invalid” index. The following tables summarise what happen when you subset a logical vector, list, and
NULL with an out-of-bounds value (OOB), a missing value (i.e
NA_integer_), and a zero-length object (like
[[. Each cell shows the result of subsetting the data structure named in the row by the type of index described in the column. I’ve only shown the results for logical vectors, but other atomic vectors behave similarly, returning elements of the same type.
[, it doesn’t matter whether the OOB index is a position or a name, but it does for
||Zero-length||OOB (int)||OOB (chr)||Missing|
If the input vector is named, then the names of OOB, missing, or
NULL components will be
The inconsistency of the
[[ table above lead to the development of
pluck() always returns
NULL (or the value of the
.default argument) when the element is missing;
chuck() always throws an error:
||Zero-length||OOB (int)||OOB (chr)||Missing|
||Zero-length||OOB (int)||OOB (chr)||Missing|
The behaviour of
pluck() makes it well suited for indexing into deeply nested data structures where the component you want does not exist always exist (as is common when working with JSON data from web APIs).
pluck() also allows you to mingle integer and character indexes, and to provide an alternative default value if the item does not exist:
There are also two additional subsetting operators that are needed for S4 objects:
@ (equivalent to
slot() (equivalent to
@ is more restrictive than
$ in that it will return an error if the slot does not exist. These are described in more detail in S4.
Brainstorm as manys way as possible to extract the third value from the
cylvariable in the
Given a linear model, e.g.,
mod <- lm(mpg ~ wt, data = mtcars), extract the residual degrees of freedom. Extract the R squared from the model summary (
5.4 Subsetting and assignment
All subsetting operators can be combined with assignment to modify selected values of the input vector.
x <- 1:5 x[c(1, 2)] <- 2:3 x #>  2 3 3 4 5 # The length of the LHS needs to match the RHS x[-1] <- 4:1 x #>  2 4 3 2 1 # Duplicated indices go unchecked and may be problematic x[c(1, 1)] <- 2:3 x #>  3 4 3 2 1 # You can't combine integer indices with NA x[c(1, NA)] <- c(1, 2) #> Error in x[c(1, NA)] <- c(1, 2): #> NAs are not allowed in subscripted assignments # But you can combine logical indices with NA # (where they're treated as false). x[c(T, F, NA)] <- 1 x #>  1 4 3 1 1 # This is mostly useful when conditionally modifying vectors df <- data.frame(a = c(1, 10, NA)) df$a[df$a < 5] <- 0 df$a #>  0 10 NA
Subsetting with nothing can be useful in conjunction with assignment because it will preserve the structure of the original object. Compare the following two expressions. In the first,
mtcars will remain as a data frame. In the second,
mtcars will become a list.
With lists, you can use
[[ + assignment +
NULL to remove components from a list. To add a literal
NULL to a list, use
The basic principles described above give rise to a wide variety of useful applications. Some of the most important are described below. Many of these basic techniques are wrapped up into more concise functions (e.g.,
dplyr::arrange()), but it is useful to understand how they are implemented with basic subsetting. This will allow you to adapt to new situations not handled by existing functions.
5.5.1 Lookup tables (character subsetting)
Character matching provides a powerful way to make lookup tables. Say you want to convert abbreviations:
If you don’t want names in the result, use
unname() to remove them.
5.5.2 Matching and merging by hand (integer subsetting)
You may have a more complicated lookup table which has multiple columns of information. Suppose we have a vector of integer grades, and a table that describes their properties:
We want to duplicate the info table so that we have a row for each value in
grades. An elegant way to do this is by combining
match() and integer subsetting:
If you have multiple columns to match on, you’ll need to first collapse them to a single column (with e.g.
interaction()), but typically you are better off switching to a function design specifically for joining multiple tables like
5.5.3 Random samples/bootstraps (integer subsetting)
You can use integer indices to perform random sampling or bootstrapping of a vector or data frame.
sample() generates a vector of indices, then subsetting accesses the values:
df <- data.frame(x = c(1, 2, 3, 1, 2), y = 5:1, z = letters[1:5]) # Randomly reorder df[sample(nrow(df)), ] #> x y z #> 1 1 5 a #> 4 1 2 d #> 2 2 4 b #> 5 2 1 e #> 3 3 3 c # Select 3 random rows df[sample(nrow(df), 3), ] #> x y z #> 3 3 3 c #> 2 2 4 b #> 1 1 5 a # Select 6 bootstrap replicates df[sample(nrow(df), 6, replace = TRUE), ] #> x y z #> 4 1 2 d #> 4.1 1 2 d #> 5 2 1 e #> 1 1 5 a #> 1.1 1 5 a #> 2 2 4 b
The arguments of
sample() control the number of samples to extract, and whether sampling is performed with or without replacement.
5.5.4 Ordering (integer subsetting)
order() takes a vector as input and returns an integer vector describing how the subsetted vector should be ordered:
To break ties, you can supply additional variables to
order(), and you can change from ascending to descending order using
decreasing = TRUE. By default, any missing values will be put at the end of the vector; however, you can remove them with
na.last = NA or put at the front with
na.last = FALSE.
For two or more dimensions,
order() and integer subsetting makes it easy to order either the rows or columns of an object:
You can sort vectors directly with
sort(), or use
dplyr::arrange() or similar to sort a data frame.
5.5.5 Expanding aggregated counts (integer subsetting)
Sometimes you get a data frame where identical rows have been collapsed into one and a count column has been added.
rep() and integer subsetting make it easy to uncollapse the data by subsetting with a repeated row index:
5.5.6 Removing columns from data frames (character subsetting)
There are two ways to remove columns from a data frame. You can set individual columns to
Or you can subset to return only the columns you want:
If you only know the columns you don’t want, use set operations to work out which colums to keep:
5.5.7 Selecting rows based on a condition (logical subsetting)
Because it allows you to easily combine conditions from multiple columns, logical subsetting is probably the most commonly used technique for extracting rows out of a data frame.
mtcars[mtcars$gear == 5, ] #> mpg cyl disp hp drat wt qsec vs am gear carb #> 27 26.0 4 120.3 91 4.43 2.14 16.7 0 1 5 2 #> 28 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2 #> 29 15.8 8 351.0 264 4.22 3.17 14.5 0 1 5 4 #> 30 19.7 6 145.0 175 3.62 2.77 15.5 0 1 5 6 #> 31 15.0 8 301.0 335 3.54 3.57 14.6 0 1 5 8 mtcars[mtcars$gear == 5 & mtcars$cyl == 4, ] #> mpg cyl disp hp drat wt qsec vs am gear carb #> 27 26.0 4 120.3 91 4.43 2.14 16.7 0 1 5 2 #> 28 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2
Remember to use the vector boolean operators
|, not the short-circuiting scalar operators
|| which are more useful inside if statements. Don’t forget De Morgan’s laws, which can be useful to simplify negations:
!(X & Y)is the same as
!X | !Y
!(X | Y)is the same as
!X & !Y
!(X & !(Y | Z)) simplifies to
!X | !!(Y|Z), and then to
!X | Y | Z.
5.5.8 Boolean algebra vs. sets (logical & integer subsetting)
It’s useful to be aware of the natural equivalence between set operations (integer subsetting) and boolean algebra (logical subsetting). Using set operations is more effective when:
You want to find the first (or last)
You have very few
TRUEs and very many
FALSEs; a set representation may be faster and require less storage.
which() allows you to convert a boolean representation to an integer representation. There’s no reverse operation in base R but we can easily create one:
Let’s create two logical vectors and their integer equivalents and then explore the relationship between boolean and set operations.
(x1 <- 1:10 %% 2 == 0) #>  FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE (x2 <- which(x1)) #>  2 4 6 8 10 (y1 <- 1:10 %% 5 == 0) #>  FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE (y2 <- which(y1)) #>  5 10 # X & Y <-> intersect(x, y) x1 & y1 #>  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE intersect(x2, y2) #>  10 # X | Y <-> union(x, y) x1 | y1 #>  FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE union(x2, y2) #>  2 4 6 8 10 5 # X & !Y <-> setdiff(x, y) x1 & !y1 #>  FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE setdiff(x2, y2) #>  2 4 6 8 # xor(X, Y) <-> setdiff(union(x, y), intersect(x, y)) xor(x1, y1) #>  FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE setdiff(union(x2, y2), intersect(x2, y2)) #>  2 4 6 8 5
When first learning subsetting, a common mistake is to use
x[which(y)] instead of
x[y]. Here the
which() achieves nothing: it switches from logical to integer subsetting but the result will be exactly the same. In more general cases, there are two important differences.
When the logical vector contains
NA, logical subsetting replaces these values by
which()drops these values. It’s not uncommon to use
which()for this side-effect, but that’s
x[-which(y)]is not equivalent to
yis all FALSE,
integer(0), so you’ll get no values, instead of all values.
In general, avoid switching from logical to integer subsetting unless you want, for example, the first or last
How would you randomly permute the columns of a data frame? (This is an important technique in random forests.) Can you simultaneously permute the rows and columns in one step?
How would you select a random sample of
mrows from a data frame? What if the sample had to be contiguous (i.e., with an initial row, a final row, and every row in between)?
How could you put the columns in a data frame in alphabetical order?
Positive integers select elements at specific positions, negative integers drop elements; logical vectors keep elements at positions corresponding to
TRUE; character vectors select elements with matching names.
[selects sub-lists. It always returns a list; if you use it with a single positive integer, it returns a list of length one.
[[selects an element within a list.
$is a convenient shorthand:
x$yis equivalent to
drop = FALSEif you are subsetting a matrix, array, or data frame and you want to preserve the original dimensions. You should almost always use it when subsetting inside a function.
xis a matrix,
x <- 0will replace every element with 0, keeping the same number of rows and columns.
x <- 0completely replaces the matrix with the value 0.
A named character vector can act as a simple lookup table:
c(x = 1, y = 2, z = 3)[c("y", "z", "x")]