Open In App
Related Articles

Create Matrix from Vectors in R

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

  1. matrix() function
  2. cbind() function
  3. rbind() function
  4. Array() function
  5. 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"


Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads