[R] Dataset to single column matrix

Rolf Turner r.turner at auckland.ac.nz
Wed Jan 8 03:41:23 CET 2014


On 08/01/14 15:09, Ista Zahn wrote:
> On Tue, Jan 7, 2014 at 8:30 PM, Pete Brecknock <Peter.Brecknock at bp.com> wrote:
>> Krishia wrote
>>> Hello,
>>> I am pretty new to R and would like to transform my 272x12 matrix into a
>>> 3264X1. I'm trying to have the setup change from:
>>>
>>> 1,  2,  3,  4, 5,  6, 7,  8, 9, 10, 11, 12
>>> 13,14,15,16,17,18,19,20,21,22, 23, 24
>>> etc.
>>>
>>> to
>>>
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8
>>> 9
>>> 10
>>> 11
>>> 12
>>> etc.
>>>
>>> Any suggestions?
>>> Thanks in advance
>>
>> Krishia
>>
>> Is this what you are looking for?
>>
>> # Create example matrice
>> m <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, byrow=TRUE)
>>
>> # Create vector
>> v <- c(t(m))
>
> Or just
>
> dim(m) <- NULL

No.  That produces column-by-column order, and it appears that the OP 
wanted row-by-row order, which is what Peter's solution gives.

BTW it is considered more "politically correct" to use as.vector(M) 
rather than c(M) to turn a matrix into a vector, although the result is 
exactly the same.

cheers,

Rolf Turner




More information about the R-help mailing list