Reputation: 3512
I would like to split a matrix which has two columns into an array. Everything I have tested so far splits it by column, e.g.
mat <- rbind(c(5, 9),
c(3, 7),
c(2, 1),
c(4, 3),
c(8, 6))
ind <- gl(1,10)
>split(mat, ind)
[1] 5 3 2 4 8 9 7 1 3 6
But the desired output is:
5 9 3 7 2 1 4 3 8 6
There must be a super easy neat trick to do this. Any pointers are highly appreciated, thanks!
Upvotes: 0
Views: 170
Reputation: 193497
You can just use as.vector
:
## what you presently have
as.vector(mat)
[1] 5 3 2 4 8 9 7 1 3 6
## What you are looking for
as.vector(t(mat))
# [1] 5 9 3 7 2 1 4 3 8 6
Upvotes: 2