2

So I have a csv file with three variables 'Team 1' 'Team 2' and 'Winner.' I want to create an adjacency matrix that has rownames=colnames. Is there any way to get this done? This is what I want:

   A B C
A  0 2 1    
B  1 0 3    
C  2 4 0

So this particular matrix would indicate that A won from B 1 time and B from A 2 times and so on. The rownames indicate the winners.

For example, if my data looks like this:

   Team A       Team B      Winner
  Germany    Argentina     Germany
  Croatia      Germany     Croatia
Argentina      Croatia   Argentina

would give the matrix

            Germany Argentina   Croatia 
Germany           0         0         1  
Argentina         1         0         0  
Croatia           0         1         0

My code

data = as.matrix(read.csv("data.csv"))

labels = unique(c(data[,1],data[,2]))
A = matrix(0, length(labels),length(labels))

rownames(A) = colnames(A) <- labels
A

This creates the adjacency matrix, but how do I fill it in?

user20650
  • 24,654
  • 5
  • 56
  • 91
Five Star
  • 43
  • 5

2 Answers2

1

You could use table to pull out the results.

First, you may want to set common levels for all teams

lvs <- sort(as.character(unique(unlist(d))))
d[] <- lapply(d, factor, levels=lvs)

Then table the data

res <- table(d[c("Team.A", "Winner")]) + table(d[c("Team.B", "Winner")])    
diag(res) <- 0
res

#            Winner
# Team.A      Argentina Croatia Germany
#   Argentina         0       0       1
#   Croatia           1       0       0
#   Germany           0       1       0

If you want a specific order, you could set the variables to factor before using table, or you can change the order after

vars <- c("Germany", "Argentina","Croatia")
res[vars, vars]

Data

d <- read.table(header=T, text="'Team A'       'Team B'      Winner
  Germany    Argentina     Germany
  Croatia      Germany     Croatia
Argentina      Croatia   Argentina")
user20650
  • 24,654
  • 5
  • 56
  • 91
0

Is this what you are after? It creates a matrix with the teams like in your first example.

x = cbind(c(0, 1, 2), c(2, 0, 4), c(1, 3, 0))

colnames(x) <- c("Germany","Argentina","Croatia")
rownames(x) <- c("Germany","Argentina","Croatia")

x
william3031
  • 1,653
  • 1
  • 18
  • 39
  • Basically I have a huge data set with just the Team names and the winners of matches. I want to convert that to an adjacency matrix. This was just a simplified example. – Five Star Oct 14 '18 at 12:31
  • There are three columns in this dataset: Team 1, Team 2 and Winner. – Five Star Oct 14 '18 at 12:33
  • Are you after something like is shown here? http://rforjournalists.com/2016/09/24/creating-premier-league-results-matrix-in-r/ – william3031 Oct 14 '18 at 12:33
  • Yes something like this, but there are no scores included. I just want to show the number of matches won. Like in the example above – Five Star Oct 14 '18 at 12:35
  • There are some examples here that might help: https://stackoverflow.com/questions/16584948/how-to-create-weighted-adjacency-list-matrix-from-edge-list – william3031 Oct 14 '18 at 12:37
  • Actually the problem is I dont know how to incorporate the 'Winner' into this matrix. Like for every victory, the point should go to the team who won – Five Star Oct 14 '18 at 12:44
  • What sort of data so you have? Match results? – william3031 Oct 14 '18 at 12:56
  • Yes, match results. – Five Star Oct 14 '18 at 12:57