7 Matrices and arrays
7.1 Matrices
Matrices are collections of numerics arranged in a two-dimensional rectangular layout
- the first argument is a vector of values
- the second specifies number of rows and columns
- R offers operators and functions for matrix algebra
## [,1] [,2]
## [1,] 3 4
## [2,] 5 3
## [3,] 7 1
7.2 Arrays
Variables of the type array are higher-dimensional matrices.
- the first argument is a vector containing the values
- the second argument is a vector specifying the depth of each dimension
7.3 Arrays
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
7.4 Selection
Subsets of matrices (and arrays) can be selected as seen for vectors.
## [1] 5 3
## [1] 17 18
7.5 apply
apply
applies another function to each level of a set dimension of an array
## [1] 1 13
## [1] 1 2 3 4
## [1] 1 5 9