jO.
jO.

Reputation: 3512

split matrix by row into single array

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

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

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

Related Questions