Paul Geeleher
2009-May-27 12:39 UTC
[R] 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. Thanks! -Paul. -- Paul Geeleher School of Mathematics, Statistics and Applied Mathematics National University of Ireland Galway Ireland
Henrique Dallazuanna
2009-May-27 13:03 UTC
[R] Sort matrix by column 1 ascending then by column 2 decending
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@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@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 [[alternative HTML version deleted]]
Linlin Yan
2009-May-27 16:19 UTC
[R] Sort matrix by column 1 ascending then by column 2 decending
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], which((1:nrow(m) %in% rows) & (m[, col]==x))))); } else { rows; } } For instance:> m <- matrix( c(2, 1, 1, 3, .5, .3, .5, .2), 4) > m[,1] [,2] [1,] 2 0.5 [2,] 1 0.3 [3,] 1 0.5 [4,] 3 0.2> m[order.matrix(m),][,1] [,2] [1,] 1 0.3 [2,] 1 0.5 [3,] 2 0.5 [4,] 3 0.2> m[order.matrix(m, c("1"=FALSE, "2"=TRUE)),][,1] [,2] [1,] 1 0.5 [2,] 1 0.3 [3,] 2 0.5 [4,] 3 0.2 Any comment is welcome! ;) On Wed, May 27, 2009 at 11:04 PM, Linlin Yan <yanlinlin82 at gmail.com> wrote:>> m <- matrix( c(2, 1, 1, 3, .5, .3, .5, .2), 4) >> m > ? ? [,1] [,2] > [1,] ? ?2 ?0.5 > [2,] ? ?1 ?0.3 > [3,] ? ?1 ?0.5 > [4,] ? ?3 ?0.2 >> m[unlist(sapply(sort(unique(m[,1])), function(x) which(m[,1]==x)[order(m[(m[,1]==x),2], decreasing=TRUE)])),] > ? ? [,1] [,2] > [1,] ? ?1 ?0.5 > [2,] ? ?1 ?0.3 > [3,] ? ?2 ?0.5 > [4,] ? ?3 ?0.2 > > On Wed, May 27, 2009 at 8:39 PM, 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. >> >
Duncan Murdoch
2009-May-27 16:48 UTC
[R] Sort matrix by column 1 ascending then by column 2 decending
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 fact that order() uses a stable sort algorithm, so just sort by the second column then the first: x <- matrix(c(2,1,1,3,.5,.3,.5,.2), ncol=2) x1 <- x[order(x[,2], decreasing=TRUE),] x2 <- x1[order(x1[,1]),] x2 2. Use the fact that your values are numeric, so negatives sort in the reverse order of positives: x[order(x[,1], -x[,2]),] 3. If the values aren't known to be numeric, convert them to numeric before using them as sort keys: x[order(xtfrm(x[,1]), -xtfrm(x[,2])),] In any of these, watch out for NA handling. My methods all put NA values last, but that might not be what you want. Duncan Murdoch> > 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. >