Open In App

How to convert matrix to list of vectors in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to convert a matrix into a list of vectors in R Programming Language.

Converting matrix into a list of vectors by Columns

Method 1: Using as.list() function

To convert columns of the matrix into a list of vectors, we first need to convert the matrix to a dataframe object, which can be done by using as.data.frame(matrix_name), which takes our matrix as an argument and returns a dataframe. Once our matrix is converted into a dataframe we can pass our newly created(by conversion) dataframe as an argument into as.list(dataframe_name) function, which converts our dataframe into a list of vectors.

Syntax

as.list(as.data.frame(matrix_name))

Example 1:

R




mat<-matrix(1:20, ncol=4)
print("Sample Matrix")
mat
  
print("List of Vectors after conversion")
list<-as.list(as.data.frame(mat))
list  


Output:

 [1] "Sample Matrix"
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
[1] "List of Vectors after conversion"
$V1
[1] 1 2 3 4 5
$V2
[1]  6  7  8  9 10
$V3
[1] 11 12 13 14 15
$V4
[1] 16 17 18 19 20

Method 2: Using split() and rep()

We pass our sample matrix and replicated elements of vectors to divide the data as the parameters into split() function, which returns our list object.

split() function is used to divide a data vector into groups as defined by the factor provided. 

Syntax: split(x, f)

Parameters:

  • x: represents data vector or data frame
  • f: represents factor to divide the data

rep() is used to replicate the elements of vectors in R programming.

Syntax: rep(x, times)

Parameter:

  • x: represents data vector or data frame
  • times: frequency of appearance

Return: Returns the replicated vectors.

Example:

R




mat<-matrix(1:20, ncol=4)
print("Sample Matrix")
mat
  
list = split(mat, rep(1:ncol(mat), each = nrow(mat)))
  
print("After converting Matrix into a List")
print(list)


Output:

[1] "Sample Matrix"
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
[1] "After converting Matrix into a List"
$`1`
[1] 1 2 3 4 5
$`2`
[1]  6  7  8  9 10
$`3`
[1] 11 12 13 14 15
$`4`
[1] 16 17 18 19 20

Converting matrix into a list of vectors by Rows

Method1: Using as.list()

The approach to use this method is the same as above but to get rows we first have to get a transpose of the matrix. The t(matrix_name) function can be used. 

Syntax: 

as.list(as.data.frame(t(matrix_name)))

Example:

R




mat<-matrix(1:20, ncol=4)
print("Sample Matrix")
mat
  
print("List of Vectors after conversion")
  
list<-as.list(as.data.frame(t(mat)))
list  


Output:

[1] "Sample Matrix"
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
[1] "List of Vectors after conversion"
$V1
[1]  1  6 11 16
$V2
[1]  2  7 12 17
$V3
[1]  3  8 13 18
$V4
[1]  4  9 14 19
$V5
[1]  5 10 15 20

Method 2: Using split() and rep()

Similarly in this method also we have to find the transpose of the matrix and rest of the approach is same as above.

Syntax

split(mat, rep(1:ncol(mat), each = nrow(mat)))

Example:

R




mat<-matrix(1:20, ncol=4)
print("Sample Matrix")
mat
  
t_mat=t(mat)
list = split(
  t_mat, rep(1:ncol(t_mat), each = nrow(t_mat)))
  
print("After converting Matrix into a List")
print(list)


Output:

[1] "Sample Matrix"
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
[1] "After converting Matrix into a List"
$`1`
[1]  1  6 11 16
$`2`
[1]  2  7 12 17
$`3`
[1]  3  8 13 18
$`4`
[1]  4  9 14 19
$`5`
[1]  5 10 15 20


Last Updated : 08 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads