[R] rank(x,y)?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jun 21 20:07:31 CEST 2006


Duncan Murdoch <murdoch at stats.uwo.ca> writes:

> Suppose I have two columns, x,y.  I can use order(x,y) to calculate a 
> permutation that puts them into increasing order of x,
> with ties broken by y.
> 
> I'd like instead to calculate the rank of each pair under the same 
> ordering, but the rank() function doesn't take multiple values
> as input.  Is there a simple way to get what I want?
> 
> E.g.
> 
>  > x <- c(1,2,3,4,1,2,3,4)
>  > y <- c(1,2,3,1,2,3,1,2)
>  > rank(x+y/10)
> [1] 1 3 6 7 2 4 5 8
> 
> gives me the answer I want, but only because I know the range of y and 
> the size of gaps in the x values.  What do I do in general?

Still not quite general, but in the absence of ties:

> z[order(x,y)]<-1:8
> z
[1] 1 3 6 7 2 4 5 8


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list