Search

Friday, October 15, 2010

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

vector(M)

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

The right way to do this seems to be:

library(data)
unmatrix(M, byrow=TRUE)

2 comments:

Anonymous said...

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

bbolker said...

I think you mean

library(gdata)

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.