C.3 Matrix Subsetting

Example matrices:

##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
##   x  y  z  w
## a 1  2  3  4
## b 5  6  7  8
## c 9 10 11 12

C.3.1 Selecting Individual Elements

Matrices are two-dimensional structures: items are aligned in rows and columns. Hence, to extract an element from a matrix, we will need two indices. Mathematically, given a matrix \(\mathbf{A}\), \(a_{i,j}\) stands for the element in the \(i\)-th row and the \(j\)-th column. The same in R:

## [1] 2
## [1] 2

C.3.2 Selecting Rows and Columns

We will sometimes use the following notation to emphasise that a matrix \(\mathbf{A}\) consists of \(n\) rows or \(p\) columns:

\[ \mathbf{A}=\left[ \begin{array}{c} \mathbf{a}_{1,\cdot} \\ \mathbf{a}_{2,\cdot} \\ \vdots\\ \mathbf{a}_{n,\cdot} \\ \end{array} \right] = \left[ \begin{array}{cccc} \mathbf{a}_{\cdot,1} & \mathbf{a}_{\cdot,2} & \cdots & \mathbf{a}_{\cdot,p} \\ \end{array} \right]. \]

Here, \(\mathbf{a}_{i,\cdot}\) is a row vector of length \(p\), i.e., a \((1\times p)\)-matrix:

\[ \mathbf{a}_{i,\cdot} = \left[ \begin{array}{cccc} a_{i,1} & a_{i,2} & \cdots & a_{i,p} \\ \end{array} \right]. \]

Moreover, \(\mathbf{a}_{\cdot,j}\) is a column vector of length \(n\), i.e., an \((n\times 1)\)-matrix:

\[ \mathbf{a}_{\cdot,j} = \left[ \begin{array}{cccc} a_{1,j} & a_{2,j} & \cdots & a_{n,j} \\ \end{array} \right]^T=\left[ \begin{array}{c} {a}_{1,j} \\ {a}_{2,j} \\ \vdots\\ {a}_{n,j} \\ \end{array} \right], \]

We can extract individual rows and columns from a matrix by using the following notation:

## [1] 1 2 3 4
## [1]  2  6 10
## x y z w 
## 1 2 3 4
##  a  b  c 
##  2  6 10

Note that by extracting a single row/column, we get an atomic (one-dimensional) vector. However, we can preserve the dimensionality of the output object by passing drop=FALSE:

##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
##      [,1]
## [1,]    2
## [2,]    6
## [3,]   10
##   x y z w
## a 1 2 3 4
##    y
## a  2
## b  6
## c 10

Now this is what we call proper row and column vectors!

C.3.3 Selecting Submatrices

To create a sub-block of a given matrix we pass two indexers, possibly of length greater than one:

##      [,1] [,2] [,3]
## [1,]    1    2    4
## [2,]    5    6    8
##   x y w
## a 1 2 4
## b 5 6 8
## [1]  3 11
##      [,1]
## [1,]    3
## [2,]   11

C.3.4 Selecting Based on Logical Vectors and Matrices

We can also subset a matrix with a logical matrix of the same size. This always yields a (flat) vector in return.

## [1]  9 10 11 12

Logical vectors can also be used as indexers:

##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    9   10   11   12
##      [,1] [,2]
## [1,]    3    4
## [2,]    7    8
## [3,]   11   12

C.3.5 Selecting Based on Two-Column Matrices

Lastly, note that we can also index a matrix A with a 2-column matrix I, i.e., A[I]. This allows for an easy access to A[I[1,1], I[1,2]], A[I[2,1], I[2,2]], A[I[3,1], I[3,2]], …

## [1]  2 11  6  1  8

This is exactly A[1, 2], A[3, 3], A[2, 2], A[1, 1], A[2, 4].