R – pmax (parallel maximum) equivalent for rank in R

r

If one has 4 judges and they each give a score for a particular performer or a particular topic then one could have 4 vectors with each containing the score.
But one would like to turn that into a rank to overcome grade inflation by one judge compared to another.
that is easy

transform(assignment,judge1.rank=rank(judge1),judge2.rank=rank(judge2),
                     judge3.rank=rank(judge3), judge4.rank=rank(judge4))

But then for each row (performer or topic) I want another four columns that for each row states the rank of ranks (or parallel rank) for each judge.

I would like to do something such as

prank(judge1.rank,judge2.rank,judge3.rank,judge4.rank)

I guess it would have to output as a dataframe.

I thought of using the reshape package to melt the data but that is just a preliminary thought.

Best Solution

If I understand you correctly, this will do what you want:

## example data
set.seed(1)
judge <- data.frame(judge1 = sample(1:10), judge2 = sample(1:10),
                    judge3 = sample(1:10), judge4 = sample(1:10))

We compute the ranks for each judge using sapply() which returns a matrix of ranks. Then we use apply rank() on the rows of this matrix to compute the performer/row ranks. A final transpose gets the result back in the required orientation.

> t(apply(sapply(judge, rank), 1, rank))
      judge1 judge2 judge3
 [1,]    1.5    1.5    3.0
 [2,]    3.0    1.5    1.5
 [3,]    1.0    2.5    2.5
 [4,]    2.0    3.0    1.0
 [5,]    1.0    2.0    3.0
 [6,]    2.5    1.0    2.5
 [7,]    3.0    2.0    1.0
 [8,]    3.0    1.0    2.0
 [9,]    3.0    1.0    2.0
[10,]    1.0    3.0    2.0

Wrap that in a function and you are good to go:

prank <- function(df, ...) {
    t(apply(sapply(df, rank, ...), 1, rank, ...))
}

Which gives:

> prank(judge)
      judge1 judge2 judge3
 [1,]    1.5    1.5    3.0
 [2,]    3.0    1.5    1.5
 [3,]    1.0    2.5    2.5
 [4,]    2.0    3.0    1.0
 [5,]    1.0    2.0    3.0
 [6,]    2.5    1.0    2.5
 [7,]    3.0    2.0    1.0
 [8,]    3.0    1.0    2.0
 [9,]    3.0    1.0    2.0
[10,]    1.0    3.0    2.0

The ... allows arguments to rank() to be passed in, for example the ties.method argument:

> prank(judge, ties = "min")
      judge1 judge2 judge3
 [1,]      1      1      3
 [2,]      3      1      1
 [3,]      1      2      2
 [4,]      2      3      1
 [5,]      1      2      3
 [6,]      2      1      2
 [7,]      3      2      1
 [8,]      3      1      2
 [9,]      3      1      2
[10,]      1      3      2
Related Question