> Dear list, > I have a data frame like: > > > log2.ratios[1:3,1:4] > ID a1 a2 a3 > 1 GS1-232B23 -0.0207500 0.17553833 0.21939333 > 2 RP11-82D16 -0.1896667 0.02645167 -0.03112333 > 3 RP11-62M23 -0.1761700 0.08214500 -0.04877000 > 4 RP11-62M23 0.2761700 -0.15214500 -0.05877000 >the 3rd and 4th rows are of the same ID, I would like to take the avarage of these two rows and get the data frame to: ID a1 a2 a3 1 GS1-232B23 -0.0207500 0.17553833 0.21939333 2 RP11-82D16 -0.1896667 0.02645167 -0.03112333 3 RP11-62M23 -0.05 -0.035 0.094135 Can anyone suggest how to deal with this (to take the ave of two rows of the same ID ) more quickly? Thanks a bunch! Allen [[alternative HTML version deleted]]
Would ?aggregate help? affy snp wrote:> >> Dear list, >> I have a data frame like: >> >> > log2.ratios[1:3,1:4] >> ID a1 a2 a3 >> 1 GS1-232B23 -0.0207500 0.17553833 0.21939333 >> 2 RP11-82D16 -0.1896667 0.02645167 -0.03112333 >> 3 RP11-62M23 -0.1761700 0.08214500 -0.04877000 >> 4 RP11-62M23 0.2761700 -0.15214500 -0.05877000 >> > > the 3rd and 4th rows are of the same ID, I would like to take the avarage > of > these > two rows and get the data frame to: > > ID a1 a2 a3 > 1 GS1-232B23 -0.0207500 0.17553833 0.21939333 > 2 RP11-82D16 -0.1896667 0.02645167 -0.03112333 > 3 RP11-62M23 -0.05 -0.035 0.094135 > > Can anyone suggest how to deal with this (to take the ave of two rows of > the > same ID ) more quickly? > > Thanks a bunch! > > Allen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/How-to-take-the-ave-of-two-rows-in-a-data-frame-tf4898908.html#a14031906 Sent from the R help mailing list archive at Nabble.com.
Try the reshape package
This should do what you want.
===============================================library(reshape)
df1 <- melt(mydata, id=c("ID"),
measured=c("a1","a2","a3")) ; df1
stats <- cast(df1, ID ~ variable , mean) ; stats
================================================
--- affy snp <affysnp at gmail.com> wrote:
> > Dear list,
> > I have a data frame like:
> >
> > > log2.ratios[1:3,1:4]
> > ID a1 a2
> a3
> > 1 GS1-232B23 -0.0207500 0.17553833
> 0.21939333
> > 2 RP11-82D16 -0.1896667 0.02645167
> -0.03112333
> > 3 RP11-62M23 -0.1761700 0.08214500
> -0.04877000
> > 4 RP11-62M23 0.2761700 -0.15214500
> -0.05877000
> >
>
> the 3rd and 4th rows are of the same ID, I would
> like to take the avarage of
> these
> two rows and get the data frame to:
>
> ID a1 a2
> a3
> 1 GS1-232B23 -0.0207500 0.17553833 0.21939333
> 2 RP11-82D16 -0.1896667 0.02645167 -0.03112333
> 3 RP11-62M23 -0.05 -0.035
> 0.094135
>
> Can anyone suggest how to deal with this (to take
> the ave of two rows of the
> same ID ) more quickly?
>
> Thanks a bunch!
>
> Allen
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
>