Pmax (parallel maximum) equivalent for rank in R
If one has 4 judges, and each of them gives a score for a specific artist or a specific topic, then each can have 4 vectors, each of which contains a score. But I would like to turn it into a rank in order to overcome class inflation by one judge over another. it's easy
transform(assignment,judge1.rank=rank(judge1),judge2.rank=rank(judge2), judge3.rank=rank(judge3), judge4.rank=rank(judge4))
But then, for each row (artist or topic), I want four more columns, which for each row define the rank of ranks (or parallel rank) for each judge.
I would like to do something like
prank(judge1.rank,judge2.rank,judge3.rank,judge4.rank)
I guess it would have to be output as a data frame.
I thought about using the reshape package to melt data, but this is just a preliminary thought.
source to share
If you read it right, 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()
that returns a rank matrix. We then use rank()
the rows of this matrix to calculate the artist / row ranks. Final transposition returns the result in the desired 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 this in a function and you're good to go:
prank <- function(df, ...) {
t(apply(sapply(df, rank, ...), 1, rank, ...))
}
What 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
...
allows you to pass arguments rank()
, for example an argument ties.method
:
> 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
source to share