Displaying 2 results from an estimated 2 matches for "columnsdecreasing".
2009 May 27
3
Sort matrix by column 1 ascending then by column 2 decending
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.
2009 Jun 01
1
installing sn package
...ng
To: Paul Geeleher <paulgeeleher@gmail.com>, r-help@r-project.org
Message-ID:
<8d4c23b10905270919w54a1e6cbred3ab67fe0f0347f@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
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=columnsDecreasing[1]);
unlist(sapply(values, function(x) order.matrix(m,
columnsDecreasing[-1], w...