2 Subsetting R objects or accesing specific elements

There are multiple methods for sub-setting R objects (vectors, matrices, data frames, lists, etc.) and each have its own uses and benefits. We will discuss each one of them. Three operators [, [[ & $ will be used.

2.1 Subsetting vectors

Let us first start sub-setting vectors, which is as we have learned, atomic object in R. To subset the vectors we will use [. For this we will use following x vector, which has 6 elements (names) each starting with alphabets A to F.

x <- c('Andrew', 'Bob', 'Chris', 'Danny', 'Edmund', 'Freddie')

2.1.1 Subsetting through a vector of positive integers

Sub-setting through positive integers will give us elements at those given position (indices). See this

# fourth element
x[4]
## [1] "Danny"
# third to fifth element
x[3:5]
## [1] "Chris"  "Danny"  "Edmund"
# first and fifth element
x[c(1,3)]
## [1] "Andrew" "Chris"

Note: Check what happens when the integer vector has repeated integers.

2.1.2 Subsetting through a vector of negative integers

Sub-setting through negative integers will give us all elements except those at given indices. See

# all elements except that at fourth
x[-4]
## [1] "Andrew"  "Bob"     "Chris"   "Edmund"  "Freddie"
# all elements except third to fifth
x[-(3:5)]
## [1] "Andrew"  "Bob"     "Freddie"
# all elements except first and fifth
x[-c(1,5)]
## [1] "Bob"     "Chris"   "Danny"   "Freddie"

Note: Try mixing sub-setting with a vector having both positive and negative integers in your console and check what happens.

2.1.3 Subsetting through a logical vector

We can also subset a given vector through another vector having logical values i.e. TRUE and FALSE. As you can understand output/result will have elements at places having TRUE only.

# First, third and fifth element only
x[c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE)]
## [1] "Andrew" "Chris"  "Danny"

Recycling is an important concept while sub-setting though a logical vector. It recycles the given logical vector up to the length of vector to be subset. Thus, x[TRUE] will give us original x only.

x[TRUE]
## [1] "Andrew"  "Bob"     "Chris"   "Danny"   "Edmund"  "Freddie"
x[c(TRUE, FALSE)] # will give elements at odd indices
## [1] "Andrew" "Chris"  "Edmund"

Note: Try to subset a vector through a logical vector having missing values i.e. NA along with TRUE and/or FALSE in your console and check what happens.

Sub-setting through logical vector is most important and used sub-setting method as we will see it subsequent chapter/sections when we will filter a vector on the basis of some conditions.

2.1.4 Subsetting through a character vector

This method is used when the given vector is named. We can pass desired names inside [] to get/filter those desired elements. See this example.

# let us create a named vector `y`
y <- setNames(1:6, LETTERS[1:6])
# display `y`
y
## A B C D E F 
## 1 2 3 4 5 6
# subset elements named `A` and `C`
y[c('A', 'C')]
## A C 
## 1 3

Note that we have used quotes in above method of sub-setting. We can use this method when we have names saved in another variable. See this

var <- c('A', 'C', 'E')
# subset those elements from `y` which are named as per `var`
y[var] # notice that since `var` is a variable, we have not used quotes.
## A C E 
## 1 3 5

Note: Similar to positive integer indexing we will get repeated values if character vector has repeated names.

y[c("A", "A", "C", "A")]
## A A C A 
## 1 1 3 1

Other two methods of indexing will not be used frequently but are important to know for debugging the code as sometimes your subset vector may be NULL or zero

2.1.5 Subsetting through nothing

Indexing through nothing i.e. simply with [] will give us original vector.

x[]
## [1] "Andrew"  "Bob"     "Chris"   "Danny"   "Edmund"  "Freddie"

2.1.6 Subsetting through Zero

Sub-setting through NULL or 0 will give us a zero length vector.

x[NULL]
## character(0)
y[0]
## named integer(0)
is.null(x[NULL])
## [1] FALSE

It is important and interesting to note here that subsetting through NULL is not NULL and is a zero length vector instead.

2.2 Subsetting Matrices and arrays

We can subset higher dimensional structures (Matrix - 2 dimensional and arrays - dimension greater than 2) using (i) multiple vectors, (ii) single vector and (iii) matrix.

Let us first create a 5x5 matrix say mat with elements named \(A_{mn}\) where m will denote row number and n will denote column number.

##      [,1]  [,2]  [,3]  [,4]  [,5] 
## [1,] "A11" "A12" "A13" "A14" "A15"
## [2,] "A21" "A22" "A23" "A24" "A25"
## [3,] "A31" "A32" "A33" "A34" "A35"
## [4,] "A41" "A42" "A43" "A44" "A45"
## [5,] "A51" "A52" "A53" "A54" "A55"

2.2.1 Indexing through Multiple vectors

This is extension of all sub-setting methods explained for a vector. In objects with higher dimensionality we will have to provide one vector for each dimension. Blank values, as you may understood (ref - sub-setting through nothing explained above) will do nothing and return that dimension complete.

# first and second row with third and fifth column
mat[1:2, c(3,5)]
##      [,1]  [,2] 
## [1,] "A13" "A15"
## [2,] "A23" "A25"
# third to fifth column, all rows
mat[,3:5]
##      [,1]  [,2]  [,3] 
## [1,] "A13" "A14" "A15"
## [2,] "A23" "A24" "A25"
## [3,] "A33" "A34" "A35"
## [4,] "A43" "A44" "A45"
## [5,] "A53" "A54" "A55"
# all columns except third
mat[, -3]
##      [,1]  [,2]  [,3]  [,4] 
## [1,] "A11" "A12" "A14" "A15"
## [2,] "A21" "A22" "A24" "A25"
## [3,] "A31" "A32" "A34" "A35"
## [4,] "A41" "A42" "A44" "A45"
## [5,] "A51" "A52" "A54" "A55"
# Odd rows, all columns
mat[c(TRUE, FALSE),]
##      [,1]  [,2]  [,3]  [,4]  [,5] 
## [1,] "A11" "A12" "A13" "A14" "A15"
## [2,] "A31" "A32" "A33" "A34" "A35"
## [3,] "A51" "A52" "A53" "A54" "A55"

The idea can be extended to a named matrix also.

# First create a named matrix
rownames(mat) <- paste0("Row", 1:5)
colnames(mat) <- paste0("Col", 1:5)
mat
##      Col1  Col2  Col3  Col4  Col5 
## Row1 "A11" "A12" "A13" "A14" "A15"
## Row2 "A21" "A22" "A23" "A24" "A25"
## Row3 "A31" "A32" "A33" "A34" "A35"
## Row4 "A41" "A42" "A43" "A44" "A45"
## Row5 "A51" "A52" "A53" "A54" "A55"
# filter desired rows/columns
mat[c("Row1"), c("Col2", "Col3")]
##  Col2  Col3 
## "A12" "A13"

In the above example you must have noticed that indexing objects with higher dimensionality may return the objects with lower dimensionality. E.g. sub-setting a matrix may return a vector. We can control the dimensionality reduction through the argument drop=FALSE which is by default TRUE and may thus introduce bugs in the code.

mat[c("Row1"), c("Col2", "Col3"), drop=FALSE]
##      Col2  Col3 
## Row1 "A12" "A13"
#check this
dim(mat[c("Row1"), c("Col2", "Col3"), drop=FALSE])
## [1] 1 2
#versus this
dim(mat[c("Row1"), c("Col2", "Col3")])
## NULL

2.2.2 Subsetting through one vector

By now it should be clear that objects with higher dimensionality like matrices, array are actually vectors at the core of r, displayed and acting like objects having more than one dimension. So sub-setting with single vector on these objects coerce the behavior of these objects as vectors only and give output exactly as shown in previous section.

mat[c(1, 10, 15, 25)]
## [1] "A11" "A52" "A53" "A55"
# OR
mat[c(TRUE, FALSE)]
##  [1] "A11" "A31" "A51" "A22" "A42" "A13" "A33" "A53" "A24" "A44" "A15" "A35"
## [13] "A55"

2.2.3 Subsetting through a matrix

We can also subset objects with higher dimensionality with integer matrix (having number of columns equal to dimensions). In other words, to subset a matrix (2D) with the help of other matrix we will need a 2 column matrix where first column will indicate row number and second column will indicate column number. See

selection_matrix <- matrix(c(1,1, # Element at Row 1 Col 1
                             2,2, # Element at Row 2 Col 2
                             3,3), # Element at Row 3 Col 3
                           ncol = 2, 
                           byrow = TRUE)
mat[selection_matrix]
## [1] "A11" "A22" "A33"

2.3 Subsetting lists

List sub-setting can be done using either [], [[]] or $. To understand the difference between these, let us consider these one by one. As done earlier let us consider a list of 4 elements - one vector, one matrix, one list and one data frame. For now let us consider that list is unnamed.

my_list <- list(
  11:20,                                                       # first element
  outer(1:4, 1:4, FUN = function(x, y) paste0('B', x, y)),     # second element
  list(LETTERS[1:8], TRUE),                                    # third element  
  data.frame(col1 = letters[1:4], col2 = 5:8)                  # fourth element
)
# display the list
my_list
## [[1]]
##  [1] 11 12 13 14 15 16 17 18 19 20
## 
## [[2]]
##      [,1]  [,2]  [,3]  [,4] 
## [1,] "B11" "B12" "B13" "B14"
## [2,] "B21" "B22" "B23" "B24"
## [3,] "B31" "B32" "B33" "B34"
## [4,] "B41" "B42" "B43" "B44"
## 
## [[3]]
## [[3]][[1]]
## [1] "A" "B" "C" "D" "E" "F" "G" "H"
## 
## [[3]][[2]]
## [1] TRUE
## 
## 
## [[4]]
##   col1 col2
## 1    a    5
## 2    b    6
## 3    c    7
## 4    d    8

2.3.1 Subsetting lists with []

Sub-setting lists with [] will always result a list containing desired element(s).

my_list[2]
## [[1]]
##      [,1]  [,2]  [,3]  [,4] 
## [1,] "B11" "B12" "B13" "B14"
## [2,] "B21" "B22" "B23" "B24"
## [3,] "B31" "B32" "B33" "B34"
## [4,] "B41" "B42" "B43" "B44"
class(my_list[1])
## [1] "list"

We can apply other ideas of vector sub-setting as explained earlier with this list sub-setting. The output will also be list containing one or more items.

2.3.2 Subsetting lists with [[]]

Sub-setting list with [[]] will return that specific item (as per index given) but the output will be of type of that specific item.

my_list[[2]]
##      [,1]  [,2]  [,3]  [,4] 
## [1,] "B11" "B12" "B13" "B14"
## [2,] "B21" "B22" "B23" "B24"
## [3,] "B31" "B32" "B33" "B34"
## [4,] "B41" "B42" "B43" "B44"
class(my_list[[4]])
## [1] "data.frame"

Notice the difference in outputs created with my_list[2] and my_list[[2]] in above 2 code blocks.

Needless to say, one cannot index/subset lists using multiple indices. Check my_list[[1:2]] in your console as the results may not be as what you think.

2.3.3 Chaining or multiple subsetting

We can further subset/index a vector/variable in R using chaining i.e. by combining one or methods as we have discussed here.

# third element of second element
my_list[[2]][3] # recall that by default matrix is by column
## [1] "B31"
# or
my_list[[2]][1:3,2:4]
##      [,1]  [,2]  [,3] 
## [1,] "B12" "B13" "B14"
## [2,] "B22" "B23" "B24"
## [3,] "B32" "B33" "B34"

2.3.4 Subsetting with $

$ is a shorthand operator: x$y is roughly equivalent to x[["y"]]. To check this let us assign our list some names.

names(my_list) <- c("first", "second", "third", "fourth")
# Now see
my_list$first
##  [1] 11 12 13 14 15 16 17 18 19 20
my_list$fourth$col2
## [1] 5 6 7 8

Notice that rules for dimensionality reduction also applies with $.

Another difference between [[ sub-setting versus $ sub-setting is partial matching (left to right only), which is possible with $ only and not with [[. See

my_list$fir
##  [1] 11 12 13 14 15 16 17 18 19 20
my_list[['fir']]
## NULL

2.4 Data frames

As already explained data frames are basically lists with each element having equal length, rules for sub-setting lists all apply with data frames. One addition is that data frames can also be subset using rules for matrix sub-setting.

mtcars # it is a default data frame in r
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
# list type sub-setting
mtcars[[2]]  # second column 
##  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
# matrix type
mtcars[1:4, 2:3] # first four rows with second & third columns
##                cyl disp
## Mazda RX4        6  160
## Mazda RX4 Wag    6  160
## Datsun 710       4  108
## Hornet 4 Drive   6  258

Remember

  1. If sub-setting data frames with single vector, data frame behave like lists, by default. If we have to get output as data.frame we will have use drop= FALSE argument.
  2. If however, sub-setting data frame through two vectors, these behave like matrices.

Examples

#Default
mtcars[1:5, 2]
## [1] 6 6 4 6 8
# using drop argument
mtcars[1:5, 2, drop = FALSE]
##                   cyl
## Mazda RX4           6
## Mazda RX4 Wag       6
## Datsun 710          4
## Hornet 4 Drive      6
## Hornet Sportabout   8

2.5 Subsetting and assignment

All the sub-setting that we have seen can be used for assignment as well.

my_list$first <- mtcars[1:4, 2:3]
my_list
## $first
##                cyl disp
## Mazda RX4        6  160
## Mazda RX4 Wag    6  160
## Datsun 710       4  108
## Hornet 4 Drive   6  258
## 
## $second
##      [,1]  [,2]  [,3]  [,4] 
## [1,] "B11" "B12" "B13" "B14"
## [2,] "B21" "B22" "B23" "B24"
## [3,] "B31" "B32" "B33" "B34"
## [4,] "B41" "B42" "B43" "B44"
## 
## $third
## $third[[1]]
## [1] "A" "B" "C" "D" "E" "F" "G" "H"
## 
## $third[[2]]
## [1] TRUE
## 
## 
## $fourth
##   col1 col2
## 1    a    5
## 2    b    6
## 3    c    7
## 4    d    8