w3resource

R Programming: Convert a given matrix to a list of column-vectors

R Programming: Matrix Exercise-10 with Solution

Write a R program to convert a given matrix to a list of column-vectors.

Sample Solution:

R Programming Code:

x = matrix(1:12, ncol=3)
print("Original matrix:")
print(x)
print("list from the said matrix:")
l =  split(x, rep(1:ncol(x), each = nrow(x)))
print(l)

Sample Output:

[1] "Original matrix:"
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
[1] "list from the said matrix:"
$`1`
[1] 1 2 3 4

$`2`
[1] 5 6 7 8

$`3`
[1]  9 10 11 12               

R Programming Code Editor:



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

Previous: Write a R program to create a correlation matrix from a dataframe of same data type.
Next: Write a R program to find row and column index of maximum and minimum value in a given matrix.

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.