-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,770
  • 3
  • 25
  • 37
Pegah
  • 121
  • 1
  • 12

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
  • 190,393
  • 28
  • 405
  • 485