[R] pairwise difference operator

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Sat Jul 31 01:30:52 CEST 2004


There was a BioConductor thread today where the poster wanted to find
pairwise difference between columns of a matrix. I suggested the slow
solution below, hoping that someone might suggest a faster and/or more
elegant solution, but no other response.

I tried unsuccessfully with the apply() family. Searching the mailing
list was not very fruitful either. The closest I got to was a cryptic
chunk of code in pairwise.table().

Since I do use something similar myself occasionally, I am hoping
someone from the R-help list can suggest alternatives or past threads.
Thank you.

### Code ###
pairwise.difference <- function(m){
  npairs  <- choose( ncol(m), 2 )
  results <- matrix( NA, nc=npairs, nr=nrow(m) )
  cnames  <- rep(NA, npairs)
  if(is.null(colnames(m))) colnames(m) <- paste("col", 1:ncol(m), sep="")
  
  k <- 1
  for(i in 1:ncol(m)){
    for(j in 1:ncol(m)){
      if(j <= i) next;
      results[ ,k] <- m[ ,i] - m[ ,j]
      cnames[k]    <- paste(colnames(m)[ c(i, j) ], collapse=".vs.")
      k <- k + 1
    }
  }
  
  colnames(results) <- cnames
  rownames(results) <- rownames(m)
  return(results)
}

### Example using a matrix with 5 gene/row and 4 columns ###
mat <- matrix( sample(1:20), nc=4 )
colnames(mat) <- LETTERS[1:4]; rownames(mat) <- paste( "g", 1:5, sep="") 
mat
    A  B  C  D
g1 10 16  3 15
g2 18  5 12 19
g3  7  4  8 13
g4 14  2  6 11
g5 17  1 20  9

pairwise.difference(mat)
   A.vs.B A.vs.C A.vs.D B.vs.C B.vs.D C.vs.D
g1     -6      7     -5     13      1    -12
g2     13      6     -1     -7    -14     -7
g3      3     -1     -6     -4     -9     -5
g4     12      8      3     -4     -9     -5
g5     16     -3      8    -19     -8     11

Regards, 
-- 
Adaikalavan Ramasamy                    ramasamy at cancer.org.uk
Centre for Statistics in Medicine       http://www.ihs.ox.ac.uk/csm/
Cancer Research UK                      Tel : 01865 226 677
Old Road Campus, Headington, Oxford     Fax : 01865 226 962




More information about the R-help mailing list