How to convert matrix to list of vectors in R ?
Last Updated :
08 May, 2021
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
Similar Reads
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
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 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
How to Convert Sparse Matrix to Dense Matrix in R? R is a programming language used for performing statistical computation and graphical purposes. It is currently supported and backed by the R Core Team and the R Foundation for Statistical Computing. For data analysis and the creation of statistical software, R Programming Language is used by statis
2 min read
How to Convert Sparse Matrix to Dense Matrix in R? R is a programming language used for performing statistical computation and graphical purposes. It is currently supported and backed by the R Core Team and the R Foundation for Statistical Computing. For data analysis and the creation of statistical software, R Programming Language is used by statis
2 min read
How Can I Convert a List of Character Vectors to a Single Vector in R? In R Language lists are a flexible data structure that can contain elements of different types, including vectors. When working with lists of character vectors, you might want to combine them into a single vector for easier manipulation or analysis. This can be done using various built-in functions
3 min read