# Convert matrix to list in R

• Last Updated : 09 May, 2021

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 function to convert our matrix to a list. These objects can be Vectors, Matrices, Factors, and data frames. By default, as.list() converts the matrix to a list of lists in column-major order.

Therefore, we have to use unlist() function to convert the list of lists to a single list. unlist() function in R Language is used to convert a list of lists to a single list, by preserving all the components.

Syntax:

unlist(as.list(matrix))

Example:

## R

 `mat = ``matrix``(1:12,nrow=3, ncol=4)`` ` `print``(``"Sample matrix:"``)``print``(mat)`` ` `print``(``"Matrix into a single list"``)``unlist``(``as.list``(mat))`

Output:

[1] “Sample matrix:”

[,1] [,2] [,3] [,4]

[1,]    1    4    7   10

[2,]    2    5    8   11

[3,]    3    6    9   12

[1] “Matrix into a single list”

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

### Conversion of a matrix into a list in Row-major order

For this approach we first have to find the transpose of the matrix In the code below, we have used t() function to calculate the transpose of our sample matrix. Due to which our matrix gets converted to a list in a Row-Major order.

The rest of the process is the same as above.

Syntax:

unlist( as.list( t(mat) ))

Example:

## R

 `mat = ``matrix``(1:12,nrow=3, ncol=4)`` ` `print``(``"Sample matrix:"``)``print``(mat)`` ` `print``(``"Result after conversion"``)``unlist``(``as.list``(``t``(mat)))`

Output:

[1] “Sample matrix:”

[,1] [,2] [,3] [,4]

[1,]    1    4    7   10

[2,]    2    5    8   11

[3,]    3    6    9   12

[1] “Result after conversion”

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

My Personal Notes arrow_drop_up