-1

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?

Nikolay K
  • 3,670
  • 3
  • 28
  • 37
Pegah
  • 111
  • 1
  • 11

1 Answers1

1

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
A5C1D2H2I1M1N2O1R2T1
  • 184,536
  • 28
  • 389
  • 466