Table of Contents
A matrix in R can be created by combining vectors of the same length either horizontally or vertically. This can be achieved using the “cbind” or “rbind” functions, respectively. For example, if we have two vectors named “x” and “y” with 5 elements each, we can create a matrix by using the command “matrix(cbind(x,y), ncol = 2)” which will result in a 5×2 matrix with “x” as the first column and “y” as the second column. Similarly, using the command “matrix(rbind(x,y), nrow = 2)” will result in a 2×5 matrix with “x” as the first row and “y” as the second row. In both cases, the vectors must be of the same length in order for the matrix to be created successfully.
Create a Matrix from Vectors in R (With Examples)
You can use one of the following two methods to quickly create a matrix from vectors in R:
Method 1: Use cbind() to bind vectors into matrix by columns
my_matrix <- cbind(vector1, vector2, vector3)
Method 2: Use rbind() to bind vectors into matrix by rows
my_matrix <- rbind(vector1, vector2, vector3)
The following examples show how to use each method in practice.
Method 1: Use cbind() to Bind Vectors into Matrix by Columns
The following code shows how to use cbind() to bind together three vectors into a matrix by columns:
#define vectors
vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
vector2 <- c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
vector3 <- c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30)
#column-bind vectors together into matrix
my_matrix <- cbind(vector1, vector2, vector3)
#view resulting matrix
my_matrix
vector1 vector2 vector3
[1,] 1 2 3
[2,] 2 4 6
[3,] 3 6 9
[4,] 4 8 12
[5,] 5 10 15
[6,] 6 12 18
[7,] 7 14 21
[8,] 8 16 24
[9,] 9 18 27
[10,] 10 20 30
#view dimensions of matrix
dim(my_matrix)
[1] 10 3
We can see that the result is a matrix with 10 rows and 3 columns, with each of the three original vectors representing a unique column.
Method 2: Use rbind() to Bind Vectors into Matrix by Rows
The following code shows how to use rbind() to bind together three vectors into a matrix by columns:
#define vectors
vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
vector2 <- c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
vector3 <- c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30)
#row-bind vectors together into matrix
my_matrix <- rbind(vector1, vector2, vector3)
#view resulting matrix
my_matrix
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
vector1 1 2 3 4 5 6 7 8 9 10
vector2 2 4 6 8 10 12 14 16 18 20
vector3 3 6 9 12 15 18 21 24 27 30
#view dimensions of matrix
dim(my_matrix)
[1] 3 10
We can see that the result is a matrix with 3 rows and 10 columns, with each of the three original vectors representing a unique row.
Note: In these examples, we chose to bind together three vectors into a matrix, but we can use this exact syntax to bind together any number of vectors we’d like into a matrix.
The following tutorials explain how to perform other common functions in R:
Cite this article
stats writer (2024). How can a matrix be created from vectors in R? Can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-matrix-be-created-from-vectors-in-r-can-you-provide-examples/
stats writer. "How can a matrix be created from vectors in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-a-matrix-be-created-from-vectors-in-r-can-you-provide-examples/.
stats writer. "How can a matrix be created from vectors in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-matrix-be-created-from-vectors-in-r-can-you-provide-examples/.
stats writer (2024) 'How can a matrix be created from vectors in R? Can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-matrix-be-created-from-vectors-in-r-can-you-provide-examples/.
[1] stats writer, "How can a matrix be created from vectors in R? Can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can a matrix be created from vectors in R? Can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
