p90 example

match_func = function(filename){
  match = read.table(filename, sep= "|")
  mat = matrix(rep(-1,5^2), nrow=5)
  rownames(mat) = c("A","B","C","D","E")
  colnames(mat) = c("A","B","C","D","E")
  for (i in 1:nrow(match)){
    mat[match[i,1], match[i,2]] = match[i,3];
  }
  return(mat)
}

match_func('~/lecture/riii/data/match.txt')
##    A  B  C  D  E
## A -1  1  3  2  0
## B  2 -1  1  3  0
## C  2  0 -1  1  5
## D  1  1  2 -1  0
## E  1  1  2  3 -1
#general case
match_func = function(filename,header=T,sep='|'){
  t = read.table(filename,header=header,sep = sep);
  mat = matrix(-1,
               nrow = length(levels(t[,1])),
               ncol = length(levels(t[,2])),
               dimnames = list( levels(t[,1]), levels(t[,2] ) ));

  for(i in 1:nrow(t)){
    mat[t[i,1], t[i,2]] = t[i,3];
  }
  return(mat)
}

match_func('~/lecture/riii/data/match.txt',F)
##    A  B  C  D  E
## A -1  1  3  2  0
## B  2 -1  1  3  0
## C  2  0 -1  1  5
## D  1  1  2 -1  0
## E  1  1  2  3 -1