[R] rank() vs SAS proc rank

Rolf Turner rolf at math.unb.ca
Tue Mar 30 23:18:11 CEST 2004


Gabor Grothendieck writes:

> <White.Denis <at> epamail.epa.gov> writes:
> > SAS proc rank has ties options of high and low that would allow
> > rank (c(1,1,2,2,2,2,3)) == 1 1 3 3 3 3 7
> > Could R support these ties.methods?

> Don't know how SAS works but for your vector:
> 
> > z <- c(1,1,2,2,2,2,3)
> > match(z,z)
> [1] 1 1 3 3 3 3 7

Ah, but if z isn't presorted, then it worketh not:

E.g.

	> w <- c(2,2,2,1,3,1,2)
	> match(w,w)
	[1] 1 1 1 4 5 4 1

Try
	> bar <- function(x) {
	 	o <- order(x)
	 	x <- x[o]
	 	match(x,x)[order(o)]
	 }

Then
	> bar(w)
	[1] 3 3 3 1 7 1 3

which ***is*** what's wanted.  I have not tested this idea any
further. :-)

				cheers,

					Rolf Turner
					rolf at math.unb.ca




More information about the R-help mailing list