R Programming: Create a Matrix From a List of Given Vectors

  • Post author:
  • Post last modified:March 22, 2022
  • Post category:R
  • Post comments:0 Comments
You are currently viewing R Programming: Create a Matrix From a List of Given Vectors

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

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)

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            

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply