Here we are explaining how to write an R program to convert a given matrix into a list. Here we are using a built-in function matrix() for this conversion. This method helps to creates a matrix from the given set of values. The syntax of this function is,
Below are the steps used in the R program to convert a given matrix into a list. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variables Matx for assigning matrix. And create the matrix. Finally use split() method which helps to divide the vector or data frame containing values into groups. The rep() method replicates the values.
Matx = matrix(1:8,nrow=2, ncol=2)
print("Original matrix:")
## [1] "Original matrix:"
print(Matx)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
L= split(Matx, rep(1:ncol(Matx), each = nrow(Matx)))
print("list from the matrix:")
## [1] "list from the matrix:"
print(L)
## $`1`
## [1] 1 2
##
## $`2`
## [1] 3 4