Matrix is on of the fundamental data structures in R. In this tutorial, we will see three key matrix functions in R, matrix(), is.matrix(), and as.matrix(). These three matrix functions help us create a new matrix from scratch, verify if an object is a matrix or not and convert a dataframe into a matrix. We will learn how to use these matrix functions with examples.
matrix() function: Create matrix from data
The first function we will see is matrix() function. matrix() function is useful to create a new matrix from a vector of numbers.
For example, if we have our data stored as a vector in R
data <- 1:10
We can create a new matrix using the data as follows. By using the data as argument to matrix() function it creates a one dimensional matrix, a matrix with one column and 10 rows.
matrix(data) ## [,1] ## [1,] 1 ## [2,] 2 ## [3,] 3 ## [4,] 4 ## [5,] 5 ## [6,] 6 ## [7,] 7 ## [8,] 8 ## [9,] 9 ## [10,] 10
By specifying the number of rows using the argument “nrow” we get a matrix of dimension 5 x 2, i.e. 5 rows and 2 columns.
matrix(data, nrow=5) ## [,1] [,2] ## [1,] 1 6 ## [2,] 2 7 ## [3,] 3 8 ## [4,] 4 9 ## [5,] 5 10
Similarly we can use “ncol” argument to matrix() function to specify the number of columns. Here we use new data with ncol=3 to create a matrix with 3 columns.
matrix(1:12, ncol=3) ## [,1] [,2] [,3] ## [1,] 1 5 9 ## [2,] 2 6 10 ## [3,] 3 7 11 ## [4,] 4 8 12
Note that, when the data size does not match with either number of rows or columns arguments, matrix() functions will recycle the values from the input data.
For example, in this example we are trying to create matrix with 5 numbers but with 4 rows.
matrix(1:5, nrow=4)
It warns us and give a matrix with four rows and two columns, but with reusing the input data. Check that starting from the second element in the second column R is reusing the input data.
## Warning in matrix(1:5, nrow = 4): data length [5] is not a sub-multiple or ## multiple of the number of rows [4] ## [,1] [,2] ## [1,] 1 5 ## [2,] 2 1 ## [3,] 3 2 ## [4,] 4 3
is.matrix() function: Check if input is a matrix or not
is.matrix() function takes an object as input and checks if it is a matrix object or not. Let us create a matrix using matrix() function as before.
mat <- matrix(1:10,nrow=5)
And we can use is.matrix() to verify that it is a matrix.
is.matrix(mat) ## [1] TRUE
is.matrix(mat) ## [1] TRUE
If we use the data variable that we created earlier, which is a vector, we can see that is.matrix() functions gives us False.
is.matrix(data) ## [1] FALSE
as.matrix() function: convert dataframe into a matrix
as.matrix() can be using convert a data frame in to matrix.
Let us use one of the built-in datasets in R that is stored as a dataframe to convert as matrix.
head(mtcars) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mtcars is a dataframe, when we check if it is a matrix using is.matrix() function we get FALSE.
is.matrix(mtcars) ## [1] FALSE
We can use as.matrix() function to convert to a matrix.
new_mat = as.matrix(mtcars)
head(new_mat) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Now if we use is.matrix() function on the result from as.matrix() we get True confirming us it is a matrix now.
is.matrix(new_mat) ## [1] TRUE