8 Subsetting
There will be occasions where you don’t want all the values in a vector/list/matrix/dataframe. Instead, you’ll only want a subset. The way to do that is slightly different depending on the data structure you’re using.
Note: In some programming languages, an index starts from 0. This means that you have a list or array or similar, the first value is at position 0, then 1, then 2, etc. In R, the first value is at position 1. In other words, we index from 1 in R.
8.1 Vectors
Vectors are simple. Just use square brackets ([]
or [[]]
) after your vector and provide the index or indices of the values that you want:
c(10,20,30,40)[1]
## [1] 10
c(10,20,30,40)[c(1,4)]
## [1] 10 40
c(10,20,30,40)[1:3]
## [1] 10 20 30
c(10,20,30,40)[[1]]
## [1] 10
P.S. If you have a vector of named values, you can also use the names instead of the indices. Like c(value_1 = 1)[["value_1"]]
.
But Adam, I hear you ask, c(10,20,30,40)[1]
and c(10,20,30,40)[[1]]
just gave us the same thing, so are the interchangeable?
Well, they kind of returned the same thing, but they didn’t. So no, they’re not interchangeable.
Essentially, []
returns the container at the provided index, where [[]]
returns the value at the provided index. Another way of thinking about it is that []
is a subsetting operator (you’re taking a subset of the original set) whereas [[]]
is an extraction operator (you’re extracting a value out of the vector). As a real-world analogy, say you have ten salt shakers lined up in a row with salt in each one. Using the []
operator will give you the salt shaker (and the salt) you asked for. Using [[]]
will give you just the salt inside the shaker, but not the shaker.
Let’s see a practical example in R of the difference:
c(value_1 = 10,
value_2 = 20)[1]
## value_1
## 10
c(value_1 = 10,
value_2 = 20)[[1]]
## [1] 10
In the first call, we get the name of the value and the value itself. In other words, rather than just returning the value at that index, we’ve essentially just chopped up the vector to only returning everything from the first position. Conversely, in the second call, we’ve just been given the value. What we’ve done here is extracted the value out from that position.
As a result of this difference, []
can be used with more that one index (e.g. [1:5]
or [c(1,3)]
) whereas [[]]
can only be used with a single index.
It’s a very subtle difference, but it is an important one. Make sure that if you want the value, use [[]]
, and if you want the whole part of the vector, use []
.
8.2 Lists
Lists can be subsetted in the same way as vectors - []
returns the container at the index provided and [[]]
returns just the value:
list(
value_1 = c(1,2,3),
value_2 = c("hello", "there", "everyone")
1]] )[[
## [1] 1 2 3
list(
value_1 = c(1,2,3),
value_2 = c("hello", "there", "everyone")
1] )[
## $value_1
## [1] 1 2 3
A key difference with lists however, is that you can also subset based on the name of the value in the list using the $
operator:
list(
value_1 = c(1,2,3),
value_2 = c("hello", "there", "everyone")
$value_1 )
## [1] 1 2 3
This is equivalent to:
list(
value_1 = c(1,2,3),
value_2 = c("hello", "there", "everyone")
"value_1"]] )[[
## [1] 1 2 3
Another key difference is that lists can, of course, hold recursive values. This means that subsetting a list can return another list, that can also be subsetted and so on:
list(
list_1 = list(
list_2 = list(
list_3 = "hello"
)
)1][1][1] )[
## $list_1
## $list_1$list_2
## $list_1$list_2$list_3
## [1] "hello"
And of course, you can do the same thing with the [[]]
operator if you only want the value and not the container.
8.3 Matrices
Matrices are two dimension, meaning they can’t be subsetted with a single value. Instead, we still use the []
operator, but we provide two values: one for the row and another for the column:
matrix(c(1:10), nrow = 5, ncol = 2)
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
matrix(c(1:10), nrow = 5, ncol = 2)[4,1]
## [1] 4
If we want all the rows (or columns), we just leave that part of the bracket blank, like this:
matrix(c(1:10), nrow = 5, ncol = 2)[,1]
## [1] 1 2 3 4 5
matrix(c(1:10), nrow = 5, ncol = 2)[4,]
## [1] 4 9
We can also subset consecutive or non-consecutive rows and columns in a single call by providing a vector of locations we want:
matrix(c(1:10), nrow = 5, ncol = 2)[,1:2] # Columns 1 to 2
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
matrix(c(1:10), nrow = 5, ncol = 2)[c(1,3),] # Rows 1 and 3
## [,1] [,2]
## [1,] 1 6
## [2,] 3 8
8.4 Dataframes
Dataframes can be subsetted in the same way as matrices (using the []
operator):
data.frame(
col_1 = c(1,2,3),
col_2 = c("hello", "there", "everybody")
1] )[,
## [1] 1 2 3
However, dataframes can also be subsetted (like lists), using the $
operator and the name of the column:
data.frame(
col_1 = c(1,2,3),
col_2 = c("hello", "there", "everybody")
$col_1 )
## [1] 1 2 3
Why does this approach work for dataframes? Well, as I alluded to before, dataframes store columns as lists. But technically, the dataframe itself is also stored as a kind of list, with each column being another entry in that list. So, just like we can subset lists using $
, we can subset dataframes with it as well because a dataframe is like a fancy list.
8.5 Subsetting by criteria
Sometimes, you might not know the indices of the items you want to extract from a datastructure. Instead, you might want to do something like “extract all numbers from a vector that are less than three”. To do this, we essentially find the indices of the values that match our criteria and then subset the data structure like we learned previously.
Let’s look at subsetting a vector as an example:
c(10,15,14,20,21,50) vector1 <-
Let’s say want to extract all of the values below 20. To find the indices of the values that match our criteria, we just use our logical operators:
< 20 vector1
## [1] TRUE TRUE TRUE FALSE FALSE FALSE
This returns TRUE if the value is less than 20, and FALSE if it isn’t. We can then pass this vector of TRUE and FALSEs in []
after the vector to only return the values we want:
< 20] vector1[vector1
## [1] 10 15 14
Other data structures can also be subsetted in the same way, but for matrices or dataframes, it’s easier to use something like subset
or dplyr::filter()
(although subset
has its own limitations).
You’ll notice that this is ever so slightly different to the way we were subsetting before. Previously, we were providing just the indices of the values we wanted (e.g. 1,2 and 4). But here, we’re actually providing a vector of TRUE and FALSE values to indicate which values we want. The structure is slightly different, but the logic is the same.
This does mean however, that you can also provide a vector of TRUEs and FALSEs yourself manually if you wish. There are two reasons why I would avoid this however:
- It takes longer to write out
- If you don’t provide the same number of logical values (i.e. TRUEs and FALSEs) as there are values in the vector, then the logical values are recycled. That means that if you have a vector that’s 6 values long, and you provide a logical vector to subset it that is only three values long, then your logical vector is going to be repeated. This can lead to unwanted results:
c(TRUE, FALSE)] vector1[
## [1] 10 14 21
Here, because I’ve only specified two logical values, when it comes to subsetting time, those two values will be recycled to create a vector like this c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
. This is why we get three values returned instead of the expected one.
So while you can manually subset with a vector of logical values indicating whether to return that value as is returned, it’s best to stay away from it.