Question

I would like to take nominal results from a round-robin tournament and convert them to a list of binary adjacency matrices.

By convention, results from these tournaments are written by recording the name of the winner. Here is code for an example table where four individuals (A,B,C,D) compete against each other:

set <- c(rep(1, 6), rep(2,6))
trial <- (1:12)
home <- c("B", "A", "C", "D", "B", "C", "D", "C", "B", "A", "A", "D")
visitor <- c("D", "C", "B", "A", "A", "D", "B", "A", "C", "D", "B", "C" )
winners.rr1 <- c("D", "A", "B", "A", "A", "D", "D", "A", "B", "D", "A", "D")
winners.rr2 <- c("D", "A", "C", "A", "A", "D", "D", "A", "C", "A", "A", "D")
winners.rr3 <- c("D", "A", "B", "A", "A", "D", "D", "A", "B", "D", "A", "D")
roundrobin <- data.frame(set=set, trial=trial, home=home, visitor=visitor,
                         winners.rr1=winners.rr1, winners.rr2=winners.rr2,
                         winners.rr3=winners.rr3)

Here's the table:

> roundrobin
   set trial home visitor winners.rr1 winners.rr2 winners.rr3
1    1     1    B       D           D           D           D
2    1     2    A       C           A           A           A
3    1     3    C       B           B           C           B
4    1     4    D       A           A           A           A
5    1     5    B       A           A           A           A
6    1     6    C       D           D           D           D
7    2     7    D       B           D           D           D
8    2     8    C       A           A           A           A
9    2     9    B       C           B           C           B
10   2    10    A       D           D           A           D
11   2    11    A       B           A           A           A
12   2    12    D       C           D           D           D

This table shows the winners from three round robin tournaments. Within each tournament, there are two sets: each player competes against all others once at home, and once as a visitor. This makes for a total of 12 trials in each round robin tournament.

So, in the first trial in the first set, player D defeated player B. In the second trial of the first set, player A defeated player C, and so on.

I would like to turn these results into a list of six adjacency matrices. Each matrix is to be derived from each set within each round robin tournament. Wins are tallied on rows as "1", and losses are tallied as "0" on rows. ("Home" and "visitor" designations are irrelevant for what follows).

Here is what the adjacency matrix from Set 1 of the first round robin would look like:

> Adj.mat.set1.rr1
  X  A  B  C  D
1 A NA  1  1  1
2 B  0 NA  1  0
3 C  0  0 NA  0
4 D  0  1  1 NA

And here is what Set 2 of the first round robin would look like:

> Adj.mat.set2.rr1
  X  A  B  C  D
1 A NA  1  1  0
2 B  0 NA  1  0
3 C  0  0 NA  0
4 D  1  1  1 NA

The latter matrix shows, for example, that player A won 2 trials, player B won 1 trial, player C won 0 trials, and player D won 3 trials.

The trick of this manipulation is therefore to convert each win (recorded as a name) into a score of "1" in the appropriate row on the adjacency matrix, while losses are recorded as "0".

Any help is much appreciated.

Was it helpful?

Solution

Here's one way to go about it, although I imagine there must be a simpler approach - perhaps involving plyr. The following splits the data frame into subsets corresponding to set, then, for each round, sets up a table of zeroes (with NA diagonal) to hold results, and finally sets "winning cells" to 1 by subsetting the table with a matrix. Output class is set to matrix to ensure matrices are presented as such.

results <- lapply(split(roundrobin, roundrobin$set), function(set) {
  lapply(grep('^winners', names(set)), function(i) {
    tab <- table(set$home, set$visitor)
    tab[] <- 0
    diag(tab) <- NA
    msub <- t(apply(set, 1, function(x) {
      c(x[i], setdiff(c(x['home'], x['visitor']), x[i]))
    }))
    tab[msub] <- 1
    class(tab) <- 'matrix'
    tab
  })
})

Results for set 1:

> results[[1]]

[[1]]

     A  B  C  D
  A NA  1  1  1
  B  0 NA  1  0
  C  0  0 NA  0
  D  0  1  1 NA

[[2]]

     A  B  C  D
  A NA  1  1  1
  B  0 NA  0  0
  C  0  1 NA  0
  D  0  1  1 NA

[[3]]

     A  B  C  D
  A NA  1  1  1
  B  0 NA  1  0
  C  0  0 NA  0
  D  0  1  1 NA
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top