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
How to Convert matrix to a list of column vectors in R ?
In this article, we will discuss how to convert a given matrix to a list of column vectors in R Programming Language. To do this we will take every column of the matrix and store that column in a list and at last print the list. It can be done in these ways: Using the split() functionUsing list() wi
3 min read
Convert Matrix to Vector in R
In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix E
3 min read
Convert matrix to list in R
In this article, we will discuss how to convert a given matrix to a List in R Programming Language. Conversion of a matrix into a list in Column-major order The as.list() is an inbuilt function that takes an R language object as an argument and converts the object into a list. We have used this func
2 min read
How to concatenate two or more vectors in R ?
In this article, we will see how to concatenate two or more vectors in R Programming Language. To concatenate two or more vectors in r we can use the combination function in R. Let's assume we have 3 vectors vec1, vec2, vec3 the concatenation of these vectors can be done as c(vec1, vec2, vec3). Als
2 min read
Convert List of Vectors to DataFrame in R
In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one
2 min read
Convert dataframe to list of vectors in R
In this article, we will learn how to convert a dataframe into a list of vectors, such that we can use columns of the dataframes as vectors in the R Programming language. Dataframe columns as a list of vectors as.list() function in R Language is used to convert an object to a list. These objects can
3 min read
How to combine two vectors in R ?
In this article, we will learn how to combine two vectors in R Programming Language. We can combine two or more vectors using function c() itself. While using function c() All arguments are coerced to a common type which is the type of the returned value. Syntax: c(...) Parameters: â¦: arguments to b
3 min read
How to convert Excel column to vector in R ?
In this article, we will be looking at the different approaches to convert the Excel columns to vector in R Programming language. The approaches to convert Excel column to vector in the R language are listed as follows: Using $-Operator with the column name.Using the method of Subsetting column.Usin
3 min read
How to Create a Vector of Zero Length in R
In this article, we are going to discuss how to create a vector of zero length in R programming language. We can create a vector of zero length by passing the datatype as a value to the vector. Syntax: vector=datatype() There are six data types and each can be used to create a vector of the required
2 min read
How to convert factor levels to list in R ?
In this article, we are going to discuss how to convert the factor levels to list data structure in R Programming Language. We can get the levels of the vector using factor() function Syntax: factor(vector) Return type: vector elements with levels. If we want to get only levels, Then we can use leve
2 min read