R Crash Course Part III

6. Matrices

A matrix is a two-dimensional data structure and only contains elements of a single class. It can be created via matrix() by defining a number of rows nrow and columns ncol of a given vector:

m <- matrix(1:6, nrow = 3, ncol = 2) 
m
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

length(m)      # shows number of elements in matrix
## [1] 6

dim(m)         # shows nrow und ncol attributes
## [1] 3 2

The colon in expression 1:6 in line 1 is the sequential operator, which in this case creates a vector of all integer values between 1 and 6.
A matrix can also be constructed from vectors by using the dim () function to define the dimensionality:

a <- 1:16
a
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16

dim(a) <- c(2,8)
a
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    3    5    7    9   11   13   15
## [2,]    2    4    6    8   10   12   14   16

Furthermore, we can connect two vectors to each other via cbind() (column bind) or rbind() (row bind). If the vectors do not have the same length, they would be padded with NA values.

x <- c(23, 44, 15, 12)
y <- c( 1,  2,  3,  4)

b <- cbind(x, y)
b
##       x y
## [1,] 23 1
## [2,] 44 2
## [3,] 15 3
## [4,] 12 4

c <- rbind(x, y)
c
##   [,1] [,2] [,3] [,4]
## x   23   44   15   12
## y    1    2    3    4

If more than two dimensions are needed, e.g., when working with remote sensing imagery, we use arrays. These behave like matrices, but have at least three dimensions (help(array)).

Indexing in matrices

Indexing in matrices behaves adequately to indexing in vectors, except that we now put two index numbers in the square brackets [] to address rows and columns. Both numbers must always be separated by a comma [line, column]. If we want all the entries from one dimension, we simply leave the corresponding slot for the index numbers empty:

m <- matrix(1:15, nrow = 5, ncol = 3) 
m
##      [,1] [,2] [,3]
## [1,]    1    6   11
## [2,]    2    7   12
## [3,]    3    8   13
## [4,]    4    9   14
## [5,]    5   10   15

m[ , 2]                     # extract second column
## [1]  6  7  8  9 10

m[3,  ]                     # extract third row
## [1]  3  8 13

m[1, c(2, 3)]               # elements of first row in 2nd and 3rd column
## [1]  6 11

Calculate with matrices

R is an equally powerful tool in terms of linear algebra. Appropriate to the vectors, whole matrices can be multiplied by a single value (scalar multiplication) or element by element. For the latter, however, the matrices necessarily need the same dimensionality dim().

m1 <- matrix(1:8, nrow = 2)
m1
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    5    7
## [2,]    2    4    6    8

m1 * 5                         # scalar multiplication
##      [,1] [,2] [,3] [,4]
## [1,]    5   15   25   35
## [2,]   10   20   30   40

m1 * m1                        # multiplication element-wise
##      [,1] [,2] [,3] [,4]
## [1,]    1    9   25   49
## [2,]    4   16   36   64

Some useful and commonly used functions:

m2 <- matrix(1:6, nrow = 2)
m2
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

colMeans(m2)               # mean of all columns
## [1] 1.5 3.5 5.5

colSums(m2)                # sum of all columns
## [1]  3  7 11

rowMeans(m2)               # mean of all rows
## [1] 3 4

rowSums(m2)                # sum of all rows
## [1]  9 12

t(m2)                      # transpose a matrix
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6

m3 <- matrix(1:6, ncol = 2)
m3 %*% m2                  # matrix multiplication
##      [,1] [,2] [,3]
## [1,]    9   19   29
## [2,]   12   26   40
## [3,]   15   33   51

Matrix multiplications assume that the inner dimensions of the two matrices are the same length (here you will find further information).

Next training session incoming: