Dear All I'm trying to sort a matrix using function order, Some thing really odd: e.g. abc<-cbind(c(1,6,2),c(2,5,3),c(3,2,1))## matrix I want to sort if I do abc[ order(abc[,3]), increasing = TRUE] the result is correct [,1] [,2] [,3] [1,] 2 3 1 [2,] 6 5 2 [3,] 1 2 3 But if I want to sort in decresing order: abc[ order(abc[,3]), decreasing = TRUE] the result is wrong [,1] [,2] [,3] [1,] 2 3 1 [2,] 6 5 2 [3,] 1 2 3 Also if I use abc[ order(abc[,3]), increasing = FALSE] it returns nothing [1,] [2,] [3,] Why is that? Many thanks Yan [[alternative HTML version deleted]]
On Wed, Apr 06, 2011 at 11:35:32AM +0100, Yan Jiao wrote:> abc<-cbind(c(1,6,2),c(2,5,3),c(3,2,1))## matrix I want to sort > > if I do > abc[ order(abc[,3]), increasing = TRUE]Jim already pointed out that the argument needs to go inside the parenthes of the order function. In addition, order has an argument called 'decreasing', but none called 'inceasing'. Finally, you are lacking a comma in your subsetting of the matrix:> abc[ order(abc[,3], decreasing=F)][1] 2 6 1 But you probably mean:> abc[ order(abc[,3], decreasing=F), ][,1] [,2] [,3] [1,] 2 3 1 [2,] 6 5 2 [3,] 1 2 3 cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan Maximus-von-Imhof-Forum 3 85354 Freising, Germany http://webclu.bio.wzw.tum.de/~pagel/
Hi: Try this: abc<-cbind(c(1,6,2),c(2,5,3),c(3,2,1)) abc[order(abc[, 1], decreasing = TRUE), ] [,1] [,2] [,3] [1,] 6 5 2 [2,] 2 3 1 [3,] 1 2 3 HTH, Dennis On Wed, Apr 6, 2011 at 3:35 AM, Yan Jiao <y.jiao@ucl.ac.uk> wrote:> Dear All > > I'm trying to sort a matrix using function order, > Some thing really odd: > > e.g. > abc<-cbind(c(1,6,2),c(2,5,3),c(3,2,1))## matrix I want to sort > > if I do > abc[ order(abc[,3]), increasing = TRUE] > > the result is correct > [,1] [,2] [,3] > [1,] 2 3 1 > [2,] 6 5 2 > [3,] 1 2 3 > > But if I want to sort in decresing order: > abc[ order(abc[,3]), decreasing = TRUE] > > the result is wrong > [,1] [,2] [,3] > [1,] 2 3 1 > [2,] 6 5 2 > [3,] 1 2 3 > > Also if I use > abc[ order(abc[,3]), increasing = FALSE] > it returns nothing > [1,] > [2,] > [3,] > > Why is that? > > > Many thanks > > Yan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
On 04/06/2011 08:35 PM, Yan Jiao wrote:> Dear All > > I'm trying to sort a matrix using function order, > Some thing really odd: > > e.g. > abc<-cbind(c(1,6,2),c(2,5,3),c(3,2,1))## matrix I want to sort > > if I do > abc[ order(abc[,3]), increasing = TRUE] > > the result is correct > [,1] [,2] [,3] > [1,] 2 3 1 > [2,] 6 5 2 > [3,] 1 2 3 > > But if I want to sort in decresing order: > abc[ order(abc[,3]), decreasing = TRUE] > > the result is wrong > [,1] [,2] [,3] > [1,] 2 3 1 > [2,] 6 5 2 > [3,] 1 2 3 > > Also if I use > abc[ order(abc[,3]), increasing = FALSE] > it returns nothing > [1,] > [2,] > [3,] > > Why is that? >Hi Yan, It is because you have put the "decreasing" argument outside the parentheses, and it is not being used in the "order" function. Jim