[R] Sorting rows of a matrix independent of each other

David Winsemius dwinsemius at comcast.net
Fri Mar 13 18:35:36 CET 2009


> Now I just need the resulting matrix:
> 2 8 9
> 4 6 7
> 1 3 5




On Mar 13, 2009, at 1:26 PM, Wacek Kusnierczyk wrote:

> Kevski wrote:
>> Oh, this seemed so simple (and I'm sure the answer will be, as  
>> usual, so
>> thanks in advance for enlightening me). I need to sort each row of  
>> a matrix
>> independent of the others. For example,
>>
>>
>    apply(matrix, 1, sort)
>
>


Actually this give the transpose of what was requested, since the  
results from apply come back as column vectors. Try a slight refinement:
 > outm <- t(apply(test,1,sort))
 > outm
      [,1] [,2] [,3]
[1,]    2    8    9
[2,]    4    6    7
[3,]    1    3    5

-- 
David Winsemius




More information about the R-help mailing list