[BioC] average replicate columns in a matrix

Wolfgang Huber whuber at embl.de
Mon Mar 28 11:06:10 CEST 2011


Hi Wendy

this is by looping, but the R code is so compact that the added value of 
a specialised function might be marginal:


cn = colnames(test)
sapply(unique(cn), function(g) rowMeans(test[,cn==g,drop=FALSE]))

        A  B
[1,] 2.5 10
[2,] 3.5 11
[3,] 4.5 12


Or also:

by(t(test), colnames(test), mean)

INDICES: A
  V1  V2  V3
2.5 3.5 4.5
------------------------------------------------------------
INDICES: B
V1 V2 V3
10 11 12

Best wishes
	Wolfgang


Il Mar/28/11 1:25 AM, Wendy Qiao ha scritto:
> I have a matrix like following
>
>> test=matrix(c(1:15),nrow=3,ncol=5)
>> colnames(test)=c("A","A","B","B","B")
>> test
>       A A B  B  B
> [1,] 1 4 7 10 13
> [2,] 2 5 8 11 14
> [3,] 3 6 9 12 15
>
> I want to calculate the average of each replicates, ie. I want the output to
> be
>       A B
> [1,] 2.5 10
> [2,] 3.5 11
> [3,] 4.5 12
>
> I can do this by looping through each level of the column name, but I was
> wondering if there is a function for calculating the average of replicates
> in one step.
>
> Thank you in advance.
> Wendy
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at r-project.org
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor

-- 


Wolfgang Huber
EMBL
http://www.embl.de/research/units/genome_biology/huber



More information about the Bioconductor mailing list