On 5/27/2009 8:39 AM, Paul Geeleher 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:
You've seen a few ways. Here are some more:
1. Use the
See also this tip on the R wiki:
http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:sort
Also available as the orderBy function in the doBy package.
Kevin Wright
On Wed, May 27, 2009 at 11:19 AM, Linlin Yan wrote:
> It's a very interesting problem. I just wrote a function for it:
>
It's a very interesting problem. I just wrote a function for it:
order.matrix <- function(m, columnsDecreasing = c('1'=FALSE), rows = 1:nrow(m))
{
if (length(columnsDecreasing) > 0)
{
col <- as.integer(names(columnsDecreasing[1]));
values <- sort(unique(m[rows, col]), decreasing=column
Nice. Works perfectly.
On Wed, May 27, 2009 at 2:03 PM, Henrique Dallazuanna 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
> wrote:
>>
>> I've got a matrix with 2 columns and n rows. I need to sort
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 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, s
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 ca
6 matches
Mail list logo