<- matrix(1:9, 3, 3)) (m1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
length(m1)
[1] 9
The length()
function works with matrices, but the results may not be that interesting. When applied to matrices, length()
returns the number of cells in the matrix.
To get the dimensions of the matrix, we use dim()
.
As with vectors, default arithmetic with R matrices works element-wise. R performs the requested operation on each pair of corresponding entries in the two matrices.
[,1] [,2] [,3]
[1,] 4 5 4
[2,] -1 3 -4
[3,] 1 -2 -4
[,1] [,2] [,3]
[1,] 5 9 11
[2,] 1 8 4
[3,] 4 4 5
[,1] [,2] [,3]
[1,] 3 1 -3
[2,] -3 -2 -12
[3,] -2 -8 -13
[,1] [,2] [,3]
[1,] 0.25 0.800000 1.75
[2,] -2.00 1.666667 -2.00
[3,] 3.00 -3.000000 -2.25
[,1] [,2] [,3]
[1,] 4 20 28
[2,] -2 15 -32
[3,] 3 -12 -36
If you’re familiar with matrix algebra and/or have some affinity for programming languages that overload their operators, the matrix arithmetic described above may seem very strange. Rather than overloading the standard operators, R defines special functions for matrix algebraic operations. For example:
%*%
: Multiplicationt()
: Transpositionsolve()
: Inversion [,1] [,2] [,3]
[1,] 7 3 -40
[2,] 11 9 -44
[3,] 15 15 -48
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[,1] [,2] [,3]
[1,] 0.161290323 -0.09677419 0.25806452
[2,] 0.064516129 0.16129032 -0.09677419
[3,] 0.008064516 -0.10483871 -0.13709677
Matrices usually obey R’s recycling rules. If you attempt to perform arithmetic between a matrix and a vector that has fewer elements than the matrix, R will try to make the lengths match by recycling the elements from the vector.
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 4 7 10
[3,] 6 9 12
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 4 10 16
[3,] 9 18 27
Warning in m1 + v2: longer object length is not a multiple of shorter object
length
[,1] [,2] [,3]
[1,] 2 6 8
[2,] 4 6 10
[3,] 4 8 10
Warning in m1 * v2: longer object length is not a multiple of shorter object
length
[,1] [,2] [,3]
[1,] 1 8 7
[2,] 4 5 16
[3,] 3 12 9
[,1] [,2]
[1,] 42 42
[2,] 42 42
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4
Warning in matrix(1:3, 4, 4): data length [3] is not a sub-multiple or multiple
of the number of rows [4]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 1
[2,] 2 3 1 2
[3,] 3 1 2 3
[4,] 1 2 3 1
[,1] [,2] [,3]
[1,] 4 5 -99
[2,] -1 3 -99
[3,] 1 -2 -4
[,1] [,2] [,3]
[1,] 4 5 -99
[2,] -1 3 -99
[3,] 88 99 -4
Sometimes, R won’t apply recycling to accommodate our sloppiness, though. For example, we can’t do any arithmetic with matrices that have different dimensions.
[,1]
[1,] 1
[2,] 3
Error in m1 + m3: non-conformable arrays
Error in m1 * m3: non-conformable arrays
We also can’t do arithmetic between a matrix and vector that contains more elements than the matrix.
Error: dims [product 9] do not match the length of object [18]
Error: dims [product 9] do not match the length of object [18]
When it comes to overwriting matrix elements, R is especially picky about what size of vector it will use to overwrite a selection of matrix elements.
[,1] [,2] [,3]
[1,] 1 1 3
[2,] 2 2 4
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 1 42 42
[2,] 2 42 42
[3,] 3 6 9
Error in m1[1:2, 2:3] <- 1:3: number of items to replace is not a multiple of replacement length
Error in m1[1:2, 2:3] <- 1:3: number of items to replace is not a multiple of replacement length
HINT: The built-in R object pi
contains the value of pi.