[BioC] Average based on group

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu May 12 18:56:36 CEST 2011


Hi,

On Thu, May 12, 2011 at 12:38 PM, Fabrice Tourre <fabrice.ciup at gmail.com> wrote:
> Thanks for your reply. But it cannot be for my purpose. In fact, there
> are two snps in the example, rs9971029 and rs9971030.

Yes, I see that now, sorry about that.

> I expect fellow output with the fellow data:
>
> 0.35 0.45 0.35 0.80 0.50 1.00 2.30 0.80 0.90 0.90
>
> You can run this example to get above value
>
> -----------------------------R code------------------------------------
> df<-structure(list(seqnames = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =
> "chr10", class = "factor"),
>    snp.id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("rs9971029",
>    "rs9971030"), class = "factor"), position = c(71916552L,
>    71916553L, 71916554L, 71916555L, 71916556L, 71916557L, 71916558L,
>    71916559L, 71916560L, 71916561L, 71916726L, 71916727L, 71916728L,
>    71916729L, 71916730L, 71916731L, 71916732L, 71916733L, 71916734L,
>    71916735L), score = c(0.1, 0.4, 0.3, 0.9, 1, 2, 4, 0.8, 0.9,
>    0.8, 0.6, 0.5, 0.4, 0.7, 0, 0, 0.6, 0.8, 0.9, 1)), .Names = c("seqnames",
> "snp.id", "position", "score"), class = "data.frame", row.names = c(NA,
> -20L))
>
> a<-df[1:10,]
> b<-df[11:20,]
> cbind(a,b)->c
> (c[,4]+c[,8])/2

So, given your original data. Without doing the your cbind(a,b) trick,
how would one know that the first row for rs9971029 should be matched
(averaged) with the first row of the rs9971030 info?

Should we assume that the data is already in order and you have the
same number of "scores" for each SNP, and that you want to take the
avg of the first elements, second elements, etc.

For instance, would this do?

R> score.matrix <- do.call(cbind, split(df$score, df$snp.id))
R> rowMeans(score.matrix)
[1] 0.35 0.45 0.35 0.80 0.50 1.00 2.30 0.80 0.90 0.90

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the Bioconductor mailing list