w3resource

R Programming: Convert a given matrix to a list

R Programming: List Exercise-13 with Solution

Write a R program to convert a given matrix to a list.

Sample Solution :

R Programming Code :

m = matrix(1:10,nrow=2, ncol=2)
print("Original matrix:")
print(m)
l = split(m, rep(1:ncol(m), each = nrow(m)))
print("list from the said matrix:")
print(l)

Sample Output:

[1] "Original matrix:"
     [,1] [,2]
[1,]    1    3
[2,]    2    4
[1] "list from the said matrix:"
$`1`
[1] 1 2

$`2`
[1] 3 4                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to convert a given dataframe to a list by rows.
Next: Write a R program to assign NULL to a given list element.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.