# Matrices
Matrices store data
# Creating matrices
Under the hood, a matrix is a special kind of vector with two dimensions. Like a vector, a matrix can only have one data class. You can create matrices using the matrix
function as shown below.
matrix(data = 1:6, nrow = 2, ncol = 3)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
As you can see this gives us a matrix of all numbers from 1 to 6 with two rows and three columns. The data
parameter takes a vector of values, nrow
specifies the number of rows in the matrix, and ncol
specifies the number of columns. By convention the matrix is filled by column. The default behavior can be changed with the byrow
parameter as shown below:
matrix(data = 1:6, nrow = 2, ncol = 3, byrow = TRUE)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
Matrices do not have to be numeric – any vector can be transformed into a matrix. For example:
matrix(data = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE), nrow = 3, ncol = 2)
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] TRUE FALSE
## [3,] TRUE FALSE
matrix(data = c("a", "b", "c", "d", "e", "f"), nrow = 3, ncol = 2)
## [,1] [,2]
## [1,] "a" "d"
## [2,] "b" "e"
## [3,] "c" "f"
Like vectors matrices can be stored as variables and then called later. The rows and columns of a matrix can have names. You can look at these using the functions rownames
and colnames
. As shown below, the rows and columns don't initially have names, which is denoted by NULL
. However, you can assign values to them.
mat1 <- matrix(data = 1:6, nrow = 2, ncol = 3, byrow = TRUE)
rownames(mat1)
## NULL
colnames(mat1)
## NULL
rownames(mat1) <- c("Row 1", "Row 2")
colnames(mat1) <- c("Col 1", "Col 2", "Col 3")
mat1
## Col 1 Col 2 Col 3
## Row 1 1 2 3
## Row 2 4 5 6
It is important to note that similarly to vectors, matrices can only have one data type. If you try to specify a matrix with multiple data types the data will be coerced to the higher order data class.
The class
, is
, and as
functions can be used to check and coerce data structures in the same way they were used on the vectors in class 1.
class(mat1)
## [1] "matrix"
is.matrix(mat1)
## [1] TRUE
as.vector(mat1)
## [1] 1 4 2 5 3 6