On Thu, May 12, 2011 at 11:33 PM, Moshe Olshansky <olshansky@wehi.edu.au>wrote:

> You can use aggregate as below:
>
> junk
>      V1        V2       V3  V4
> 1  chr10 rs9971029 71916552 0.1
> 2  chr10 rs9971029 71916553 0.4
> 3  chr10 rs9971029 71916554 0.3
> 4  chr10 rs9971029 71916555 0.9
> 5  chr10 rs9971029 71916556 1.0
> 6  chr10 rs9971029 71916557 2.0
> 7  chr10 rs9971029 71916558 4.0
> 8  chr10 rs9971029 71916559 0.8
> 9  chr10 rs9971029 71916560 0.9
> 10 chr10 rs9971029 71916561 0.8
> 11 chr10 rs9971030 71916726 0.6
> 12 chr10 rs9971030 71916727 0.5
> 13 chr10 rs9971030 71916728 0.4
> 14 chr10 rs9971030 71916729 0.7
> 15 chr10 rs9971030 71916730 0.0
> 16 chr10 rs9971030 71916731 0.0
> 17 chr10 rs9971030 71916732 0.6
> 18 chr10 rs9971030 71916733 0.8
> 19 chr10 rs9971030 71916734 0.9
> 20 chr10 rs9971030 71916735 1.0
> > colnames(junk) <- c("chr","group","someNumber","value")
> > aggregate(junk$value,list(junk$group),mean)
>    Group.1    x
> 1 rs9971029 1.12
> 2 rs9971030 0.55
>
>

For many groups, this is way faster:

rowsum(junk$value, junk$group) / table(junk$group)


> > Dear list,
> > I have dataframe, the second column is groups factor, each group has
> > 10 items. The data as fellow.
> > chr10   rs9971029   71916552  0.1
> > chr10   rs9971029   71916553  0.4
> > chr10   rs9971029   71916554  0.3
> > chr10   rs9971029   71916555  0.9
> > chr10   rs9971029   71916556  1
> > chr10   rs9971029   71916557  2
> > chr10   rs9971029   71916558  4
> > chr10   rs9971029   71916559  0.8
> > chr10   rs9971029   71916560  0.9
> > chr10   rs9971029   71916561  0.8
> > chr10   rs9971030   71916726  0.6
> > chr10   rs9971030   71916727  0.5
> > chr10   rs9971030   71916728  0.4
> > chr10   rs9971030   71916729  0.7
> > chr10   rs9971030   71916730  0
> > chr10   rs9971030   71916731  0
> > chr10   rs9971030   71916732  0.6
> > chr10   rs9971030   71916733  0.8
> > chr10   rs9971030   71916734  0.9
> > chr10   rs9971030   71916735  1
> >
> > I want to get a average of each item based on the group factor. So at
> > last I want to get a vector which length is 10.
> > The value calculated as this:
> >
> > (0.1+0.6)/2
> > (0.4+0.5)/2
> > ?
> > (0.8+1)/2
> >
> > Thank you very much in advance.
> >
> > _______________________________________________
> > Bioconductor mailing list
> > Bioconductor@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/bioconductor
> > Search the archives:
> > http://news.gmane.org/gmane.science.biology.informatics.conductor
> >
>
>
>
> ______________________________________________________________________
> The information in this email is confidential and inte...{{dropped:13}}

