Open In App

Create a matrix from a list of vectors using R

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()  # 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’.

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:

do.call(rbind, x)

Where




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’

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(data, nrow, ncol, byrow, dimnames)

Where,




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’.

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.


Article Tags :