I want to extract all elements of a matrix and put them in a vector row-wise. For example, if my matrix is:
[,1] [,2] [,3]
1 2 3
4 5 6
then, I want to have a vector like this:
[1, 2, 3, 4, 5, 6]
How should I do this in R?
Just use c(t(yourmatrix))
:
m <- matrix(1:6, ncol = 3, byrow = TRUE)
m
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
c(t(m))
# [1] 1 2 3 4 5 6