Create Matrix from Vectors in R
Last Updated :
12 Jul, 2025
In this article, we will discuss How to convert a vector to a matrix in R Programming Language. A vector is a basic object that consists of homogeneous elements. The data type of vector can be integer, double, character, logical, complex, or raw. A vector can be created by using the c() function.
Syntax:
x <- c(val1, val2, .....)
To convert a vector to a matrix in R are the same as the arrays in C language which are used to hold multiple data values of the same type. Vectors can also be used to create matrices.
Matrices can be created with the help of Vectors by using pre-defined functions in R Programming Language. These functions take vectors as arguments along with several other arguments for matrix dimensions, etc. Functions used for Matrix creation:
- matrix() function
- cbind() function
- rbind() function
- Array() function
- Diag() function
matrix() function
The available data is present in single/multiple vectors, then the matrix() function can be used to create the matrix by passing the following arguments in the function to convert a vector to a matrix in R .
Syntax:
matrix(data, nrow, ncol, byrow, dimnames)
where,
data is the input vector which represents the elements in the matrix
nrow specifies the number of rows to be created
ncol specifies the number of columns to be created
byrow specifies logical value. If TRUE, matrix will be filled by row. Default value is FALSE.
dimnames specifies the names of rows and columns
R
# defining data in the vector
x <- c(5:16)
# defining row names and column names
rown <- c("row_1", "row_2", "row_3")
coln <- c("col_1", "col_2", "col_3", "col_4")
# creating matrix
m <- matrix(x, nrow = 3, byrow = TRUE,
dimnames = list(rown, coln))
# print matrix
print(m)
# print class of m
class(m)
Output:
col_1 col_2 col_3 col_4
row_1 5 6 7 8
row_2 9 10 11 12
row_3 13 14 15 16[1] "matrix" "array"
If a matrix is to be created using data present in multiple vectors, then a vector of multiple vectors is created and passed to the matrix() function as an argument to convert a vector to a matrix in R .
R
# defining multiple vectors
x <- c(1:5)
y <- c(11:15)
z <- c(21:25)
# creating matrix
m <- matrix(c(x, y, z), ncol = 3)
# print matrix
print(m)
# print class of m
class(m)
Output:
[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25[1] "matrix" "array"
cbind() function
cbind() function in R programming is used to combine vectors, data frames, or matrices by columns and the number of rows of data sets should be equal otherwise, the output will be incomprehensible to convert a vector to a matrix in R.
Syntax:
cbind(v1, v2, v3, ....., deparse.level)
where,
v1, v2, v3, .... represent vectors, matrices or data frames deparse.level used for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.
R
# defining multiple vectors
x <- c(1:5)
y <- c(11:15)
z <- c(21:25)
# creating matrix
m <- cbind(x, y, z)
# print matrix
print(m)
# print class of m
class(m)
Output:
x y z
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25[1] "matrix" "array"
rbind() function
rbind() function in R programming is used to combine vectors, data frames or matrices by rows and number of columns of data sets should be equal otherwise, output will be insignificant to convert a vector to a matrix in R.
Syntax:
rbind(v1, v2, v3, ....., deparse.level)
where,
v1, v2, v3, .... represent vectors, matrices or data frames deparse.level used for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.
R
# defining multiple vectors
x <- c(1:5)
y <- c(11:15)
z <- c(21:25)
# creating matrix
n <- rbind(x, y, z)
# print matrix
print(n)
# print class of m
class(n)
Output:
[,1] [,2] [,3] [,4] [,5]
x 1 2 3 4 5
y 11 12 13 14 15
z 21 22 23 24 25[1] "matrix" "array"
Array() function
The array() function can be used to create a matrix from vectors, as well as higher dimensional arrays to convert a vector to a matrix in R.
Syntax:
array(data, dim)
where,
- data: The input vector or data that will be used to create the array.
- dim: The dimension of the array. It can be a vector of integers indicating the number of elements in each dimension.
R
# defining multiple vectors
x <- c(1:5)
y <- c(11:15)
z <- c(21:25)
# Using the array() function
n=array(c(x, y, z), dim = c(5, 3))
# print class of m
class(n)
Output:
[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25[1] "matrix" "array"
Diag() function:
The diag() function can be used to create to convert a vector to a matrix in R.
Syntax:
diag(x, nrow, ncol)
where,
- x: a vector that will be placed on the diagonal of the resulting matrix. If x is a scalar, it will be repeated to create a vector of the required length.
- nrow: the number of rows in the resulting matrix.
- ncol: the number of columns in the resulting matrix.
R
x <- c(1:5)
y <- c(11:15)
z <- c(21:25)
n=diag(c(x, y, z))
print(n)
class(n)
Output:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0
[8,] 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0
[11,] 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0
[12,] 0 0 0 0 0 0 0 0 0 0 0 22 0 0 0
[13,] 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0
[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 24 0
[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25[1] "matrix" "array"
Similar Reads
How to create a matrix in R In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i
3 min read
Create a matrix from a list of vectors using R In R, vectors can be integer, double, character, logical, complex and raw types. A vector is created using the c() function:a <- c(val1, val2, ...Creating a List of VectorsA list of vectors in R is an array of more than one vector kept in a list structure. Creating a list of vectors is done in th
2 min read
Create a numeric vector in R A one-dimensional array containing numerical data is called a numeric vector in R Programming Language. Numerical values, such as integers or real numbers, are often stored and manipulated using numerical vectors. Concepts related to the topicNumerical Data Types: Real and integer numbers may be rep
2 min read
Convert a vector into a diagonal matrix in R In this article, we will examine various methods to convert a vector into a diagonal matrix by using R Programming Language. What is a diagonal matrix?A diagonal matrix is a special type of square matrix where all elements, except those on the main diagonal (running from the top-left to the bottom-r
3 min read
How To Remove Duplicates From Vector In R A vector is a basic data structure that is used to represent an ordered collection of elements of the same data type. It is one-dimensional and can contain numeric, character, or logical values. It is to be noted that the vector in C++ and the vector in R Programming Language are not the same. In C+
4 min read
Convert DataFrame to vector in R In this article, we will discuss how a dataframe can be converted to a vector in R. For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]]. Approach: We are taking a column in the dataframe and passing it into another variable by the selection method
2 min read