Write a R program to 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)
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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 4need an explanation for this answer? contact us directly to get an explanation for this answer