Skip to content

rank() vs SAS proc rank

4 messages · White.Denis@epamail.epa.gov, Brian Ripley, Gabor Grothendieck

#
SAS proc rank has ties options of high and low that would allow
producing ranks of the type found in the sports pages, e.g.,

rank (c(1,1,2,2,2,2,3)) == 1 1 3 3 3 3 7

Could R support these ties.methods?
#
<White.Denis <at> epamail.epa.gov> writes:
Don't know how SAS works but for your vector:
[1] 1 1 3 3 3 3 7
#
On Tue, 30 Mar 2004 White.Denis at epamail.epa.gov wrote:

            
Yes, it is possible to program them in R.  Here is one way:

1 + rowSums(outer(x, x, ">"))

which at least generalizes your single unexplained example.
#
This afternoon (EST) there were solutions to two different problems,
which on reflection have a similarity:

Prof Brian Ripley's rank variation:

  1 + rowSums(outer(x, x, ">"))

and my unique row counts:

  "%all.==%" <- function(a,b)apply(b,2,function(x)apply(t(a) == x,2,all))
  colSums( A %all.==% t(unique(A)) )

It occurred to me that if we define the APL-style
generalized matrix multiply like this:

inner <- function(a,b,f,g){ 
		f <- match.fun(f)
		g <- match.fun(g)
                apply(b,2,function(x)apply(g(t(a),x),2,f))
}

then both problems can be put into a similar form:

   1+inner( t(w), t(w), sum, "<" )

and

   colSums( inner( A, t(unique(A)), all, "==" ) )