[R] Sort matrix by column 1 ascending then by column 2 decending

Paul Geeleher paulgeeleher at gmail.com
Wed May 27 16:11:15 CEST 2009


Nice. Works perfectly.

On Wed, May 27, 2009 at 2:03 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try this:
>
> cbind(sort(x[,1]), unlist(tapply(x[,2], x[,1], sort, decreasing = T)))
>
> On Wed, May 27, 2009 at 9:39 AM, Paul Geeleher <paulgeeleher at gmail.com>
> wrote:
>>
>> I've got a matrix with 2 columns and n rows. I need to sort it first
>> by the values in column 1 ascending. Then for values which are the
>> same in column 1, sort by column 2 decending. For example:
>>
>> 2 .5
>> 1 .3
>> 1 .5
>> 3 .2
>>
>> Goes to:
>>
>> 1 .5
>> 1 .3
>> 2 .5
>> 3 .2
>>
>> This is easy to do in spreadsheet programs but I can't seem to work
>> out how to do it in R and haven't been able to find a solution
>> anywhere.
>>
>>
>> Thanks!
>>
>> -Paul.
>>
>> --
>> Paul Geeleher
>> School of Mathematics, Statistics and Applied Mathematics
>> National University of Ireland
>> Galway
>> Ireland
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



-- 
Paul Geeleher
School of Mathematics, Statistics and Applied Mathematics
National University of Ireland
Galway
Ireland




More information about the R-help mailing list