Calculate average ranking in R

Here is short post to describe how to calculate the average rank from a set of multiple ranks listed in different
data.frames in R. This is a fairly straightforward procedure, however, it took me more time than I anticipated to make this
work.

To begin with, let's create a set of data.frames with and randomly assign them rank values from 1 to 5 for the letters
A,B,C,D,E:

data.frames <- lapply(1:5, function(X) {
  data.frame(name=LETTERS[1:5], rank=sample(1:5))
})
## [[1]]
##   name rank
## 1    A    2
## 2    B    3
## 3    C    4
## 4    D    1
## 5    E    5
##
## [[2]]
##   name rank
## 1    A    4
## 2    B    3
## 3    C    5
## 4    D    2
## 5    E    1
##
## [[3]]
##   name rank
## 1    A    2
## 2    B    5
## 3    C    1
## 4    D    4
## 5    E    3
##
## [[4]]
##   name rank
## 1    A    2
## 2    B    5
## 3    C    3
## 4    D    4
## 5    E    1
##
## [[5]]
##   name rank
## 1    A    5
## 2    B    3
## 3    C    1
## 4    D    4
## 5    E    2

For A, B, C, D and E, we can quite easily calculate the average ranks. To do this, using the sapply command, we can create a
matrix of all the rankings for each data frame with a column for each of the five sets of rankings and a row for each of A
through E:

values <- sapply(data.frames, function(df) {
  df$rank
})
row.names(values) <- data.frames[[1]]$name
colnames(values) <- 1:5
##   1 2 3 4 5
## A 2 4 2 2 5
## B 3 3 5 5 3
## C 4 5 1 3 1
## D 1 2 4 4 4
## E 5 1 3 1 2

Next, we calculate the mean for each of A, B, C, D and E using the built-in R function rowMeans:

mean.of.ranks <- rowMeans(values)
##   A   B   C   D   E
## 3.0 3.8 2.8 3.0 2.4

Finally we use the order function to get the final rank values and convert the vector back into a data.frame which is of
the same format as the original rank data.frames:

ranks <- order(mean.of.ranks)
avg.rank <- data.frame(name=LETTERS[1:5], rank=ranks)
##   name rank
## 1    A    5
## 2    B    3
## 3    C    1
## 4    D    4
## 5    E    2

I hope this was of use to someone, even if that person happens to a forgetful future me. I'm more than certain that
there is an R function or an R package which will perform this for you but it is nonetheless at most interesting and at
least fun to implement. If anyone has an alternative, more elegant solution, I would really appreciate hearing from you.
Happy hacking!

Leave a Reply

Your email address will not be published. Required fields are marked *