[BioC] sort the difference and save to individual files problem

Fri Jul 30 13:31:55 CEST 2004

What you are looking for is pairwise difference between all pairs of
columns. Also you only have choose(n, 2) = n * (n-1) / 2 pairs which in
your case means 45 pairs not 100.

The below works but is inefficient for large number of columns. I would
be interested if anyone can suggest how to rewrite this using the
apply() family. [Is this question more appropriate for R-help ?]

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
return(results)
}

# Example
mat <- matrix( sample(1:12), nc=4 )
colnames(mat) <- LETTERS[1:4]
mat
A  B C  D
[1,] 10  6 3  5
[2,]  7 11 2 12
[3,]  1  8 9  4

pairwise.difference(mat)
A.vs.B A.vs.C A.vs.D B.vs.C B.vs.D C.vs.D
[1,]      4      7      5      3      1     -2
[2,]     -4      5     -5      9     -1    -10
[3,]     -7     -8     -3     -1      4      5

It is more efficient to store 1 file with 45 columns than 45 files with
one column.

On Fri, 2004-07-30 at 11:14, Dr_Gyorffy_Balazs wrote:
> I have a table with gene expression data:
>
> 		Smpl 1	Smpl 2	Smpl 3
> Gene 1	2		3		2
> Gene 2	4		6		8
> Gene 3	6		9		10
> ?
>
> [1.] I would like to construct a table/list of differences
> of every sample minus every other sample. For example:
>
> Smpl 2 - Smpl 3:
>
> Gene 1  	1
> Gene 2  	-2
> Gene 3 	-1
> ...
>
> [2.] I would like to sort all these tables/lists
> decreasingly
>
> Sample 2 ? Sample 3
>
> Gene 1  	1
> Gene 3  	-1
> Gene 2 	-2
> ...
>
> [3.] I have all together 10 samples, so the expected
> outputs are 10x10=100 tables with one column. I would like
> to save this result in 100 files (for example in a tab
> separated output).
>
> Is this possible?
>
> I can do every individual sample vs other sample by simple
> R commands (-, sort, etc), but that?s just too much work.
> Maybe somenone can help me to make this in an elegant way.
>
> (I want to compare already calculated mean values of the
> samples without additional significance tests. Therefore I
> don?t want to use sam, t-test, or other statistical
> method.)
>
> Thank you for the help
> Balazs Györffy
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor
>