[R] Sorting problem

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Mar 30 03:52:08 CEST 2009


Here's a clue:

> rank(a)
[1] 1 3 4 7 2 5 6
> order(order(a))
[1] 1 3 4 7 2 5 6

W. 


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jun Shen
Sent: Monday, 30 March 2009 10:14 AM
To: Stavros Macrakis
Cc: r-help at r-project.org; Ben Bolker
Subject: Re: [R] Sorting problem

That's leading to another question: How does rank() work?

If I have a character vector
 a<- c("2a", "2c", "3",  "5" , "2b" ,"4a", "4b")
Then a[order(a)] returns
"2a" "2b" "2c" "3"  "4a" "4b" "5", which makes sense
But a[rank(a)] returns
 "2a" "3"  "5"  "4b" "2c" "2b" "4a", which does not seem to make sense.

Similarly, for a numeric vector
b<-c( 2,  3,  1 , 6 , 5, 10,  4 , 7 , 9 , 8)
b[order(b)] returns  1  2  3  4  5  6  7  8  9 10
b[rank(b)] returns  3  1  2 10  5  8  6  4  9  7

Any explanation? Thanks.



On Sun, Mar 29, 2009 at 5:42 PM, Stavros Macrakis <macrakis at alum.mit.edu>wrote:

> On Sun, Mar 29, 2009 at 5:21 PM, Ben Bolker <bolker at ufl.edu> wrote:
> >  Adding
> >
> > xtfrm.numeric <- function(x) {x}
> >
> > would seem to add the case that people are looking for -- unless
> > there's something special that needs to be handled with NAs ???
>
> Yes, that was what I was suggesting.  xtfrm currently converts NaN to
> NA, but that does not seem to be necessary for it to meet its
> specification of "producing a numeric vector which will sort in the
> same order as 'x'", since sort treats NaNs the same as NAs.
>
>        -s
>
> ______________________________________________
> 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.
>



-- 
Jun Shen PhD
PK/PD Scientist
BioPharma Services
Millipore Corporation
15 Research Park Dr.
St Charles, MO 63304
Direct: 636-720-1589

	[[alternative HTML version deleted]]

______________________________________________
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.




More information about the R-help mailing list