Open In App

Create a matrix from a list of vectors using R

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how we can convert the list of vectors into a matrix using R Programming Language. If you want to convert simple vectors to a matrix not the list of vectors. The data type of vector can be integer, double, character, logical, complex, or raw. A vector can be created by using the c() function.

To create a vector of homogenous elements we have to use this syntax.

a<-c(val1,val2,....)

To convert a list vectors 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. For storing a List of vectors we have to create a 2D list just like a 2D array in C and C++.

List of Vectors

Before creating a matrix from the list of vectors, let’s understand how to create a list of vectors using R.

  • A list of vectors is nothing but a 2D list just like 2D arrays in C and C++ to store the homogenous elements. Just like we store elements in the 2D array we will list vectors using the List data type in R. Matrices can be created using vectors and a list of vectors but understanding how to create a list that will store a list of vectors is crucial.
  • Now, we are going to look into how we can store a list of vectors and later use that list to create a matrix.

R




a <- list()  # Create an empty list 'a'
i <- 1       # Initialize i to 1
j <- 2       # Initialize j to 2
  
# Loop from k=1 to k=5
for (k in 1:5) {
  a[[k]] <- c(i:j)  # Create a vector from i to j and assign it to the k-th element 
  i <- i + 1        # Increment i by 1
  j <- j + 1        # Increment j by 1
}
  
# Print the result
print("The list of vectors is: ")
print(a)


Output:

[1] "The list of vectors is: "
[[1]]
[1] 1 2

[[2]]
[1] 2 3

[[3]]
[1] 3 4

[[4]]
[1] 4 5

[[5]]
[1] 5 6

We initially created an empty list and stored it in the variable name ‘a’.

  • Then, we create two variables to create vectors of different data values that will keep increasing by 1 till the loop stops executing.
  • Now, we are storing a list of vectors in list ‘a’ just like 2D arrays in C and C++.
  • Then at last printing our list of vectors.

As you can see we have stored a different list of vectors each time, that’s why we created those two variables to ease the task.

Matrix from List of Vectors

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. Using R inbuilt functions we are going to create a matrix and pass a list of vectors as an argument.

The inbuilt function we are going to use to create a matrix from a list of vectors are.

  1. do.call()
  2. matrix()

Now let’s understand the workings of these functions and start creating matrices from them.

do.call() is used to call a function with a list of arguments. This function takes two arguments:

  • A specific function to call
  • list of arguments to be passed to that function which is being called.
do.call(rbind, x)

Where

  • rbind -> is the function we are calling which is used to bind objects by row
  • x -> is the 2D list that stores the list of vectors

R




a<-list()
  
i<-1
  
j<-4
  
for(k in 1:5){
  a[[k]]<-c(i:j)
    
  i<-i+1
  
  j<-j+1
}
  
do.call(rbind,a)


Output:

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    2    3    4    5
[3,]    3    4    5    6
[4,]    4    5    6    7
[5,]    5    6    7    8

Created an empty list and stored it in a variable named ‘a’

  • Declared two variables ‘i’ and ‘j’ to store vectors in the list but changing the initialization and ending. That is by increasing the variable by 1 each time
  • Finally, we call the do.call() function and pass the arguments as rbind and a.

matrix()

In the R language, the matrix() function is used to create a matrix. A matrix is a 2d data structure that is used to store values that are the same at two points that intersect each other.

  • matrix() takes single or multiple vectors to create a matrix, but here we are using a list of vectors. So, we convert a list of vectors into single or multiple vectors instead of a list of vectors.
  • This function takes arguments and has the syntax as follows
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




ug<-list()
  
i<-1
j<-5
  
for (k in 1:6){
  
  ug[[k]]<-c(i:j)
    
  i<-i+1
  j<-j+1
      
  
  
#creating list of vectors basically a 2d list
  
matrix(unlist(ug), byrow=TRUE, nrow=length(ug) )


Output:

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 3 4 5 6
[3,] 3 4 5 6 7
[4,] 4 5 6 7 8
[5,] 5 6 7 8 9
[6,] 6 7 8 9 10

We created an empty list and stored it in the variable name ‘ug’.

  • Declared two variables to change the range of vectors to store it in the list ‘ug’.
  • unlist() – This function converts the list of vectors or 2d list into a one-dimensional vector, as we have discussed earlier matrix() takes single/multiple vectors but not a list of vectors. That is why we use it to convert the ‘ug’ to single dimensional vector.
  • nrow=(length(ug)) – we set the number of rows equal to the number of rows in our list ‘ug’ which is 6.

Conclusion

Creating matrix from a list of vectors is just as simple as it is to create a matrix using a vector. R programming language is specifically used to deal with mathematical problems and data visualization. R language helps to solve complex mathematical problems with ease with the help of its inbuilt powerful functions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads