Shravan Vasishth's Slog (Statistics blog)

Friday, October 15, 2010

I foolishly tried to convert a matrix M to a vector using vector:


But this is done column wise, so that adjacent row items end up non-adjacent.

The right way to do this seems to be:

unmatrix(M, byrow=TRUE)


Anonymous said...

Or you could simply do as.vector(t(M))

bbolker said...

I think you mean


c(t(M)) works too.
The advantage of unmatrix(), according to the docs, is that it gives the resulting vector appropriate names. (If you look at the code of unmatrix() you'll see that most of it is taken up with constructing names: the actual data manipulation is done via c(x) or c(t(x)) depending on whether byrow is FALSE or TRUE.