w3resource

R Programming: Create a matrix from a list of given vectors

R Programming: Matrix Exercise-6 with Solution

Write a R program to create a matrix from a list of given vectors.

Sample Solution:

R Programming Code:

l = list()
for (i in 1:5) l[[i]] <- c(i, 1:4)
print("List of vectors:")
print(l)
result = do.call(rbind, l)
print("New Matrix:")
print(result)

Sample Output:

[1] "List of vectors:"
[[1]]
[1] 1 1 2 3 4

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

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

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

[[5]]
[1] 5 1 2 3 4

[1] "New Matrix:"
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    2    3    4
[2,]    2    1    2    3    4
[3,]    3    1    2    3    4
[4,]    4    1    2    3    4
[5,]    5    1    2    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 create two 2x3 matrix and add, subtract, multiply and divide the matrixes.
Next: Write a R program to extract the submatrix whose rows have column value > 7 from 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.