Dear all, I have a dataframe with 132 columns and 100 rows. Every 2nd column is a repeat measurement so that the columns could be titled, a a b b c c d d etc. I would like to average the repeats such that I am left with a data frame of 66 columns (of means) and 100 rows. I have been trying to use rowMeans but have not been able to average the pairs of columns, only the whole dataframe. Any help would be appreciated, I am new to R. Thanks in advance, Jon. -- View this message in context: http://www.nabble.com/averaging-pairs-of-columns-in-a-dataframe-tp19181319p19181319.html Sent from the R help mailing list archive at Nabble.com.
Try this: # test data DF <- data.frame(a = 1:9, a = 21:29, b = 31:39, b = 41:49, check.names = FALSE) t(apply(DF, 1, tapply, names(DF), mean)) It gives a matrix so use as.data.frame on that if you need a data frame On Wed, Aug 27, 2008 at 9:47 AM, JonD <jonathan.denniss at googlemail.com> wrote:> > Dear all, > > I have a dataframe with 132 columns and 100 rows. Every 2nd column is a > repeat measurement so that the columns could be titled, a a b b c c d d etc. > I would like to average the repeats such that I am left with a data frame of > 66 columns (of means) and 100 rows. > > I have been trying to use rowMeans but have not been able to average the > pairs of columns, only the whole dataframe. > > Any help would be appreciated, I am new to R. > > Thanks in advance, > > Jon. > -- > View this message in context: http://www.nabble.com/averaging-pairs-of-columns-in-a-dataframe-tp19181319p19181319.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
somehow robust method for dataframe df would be newdf <- (df[,seq(1,66,2)]+df[,seq(2,66,2)])/2 Gasper -----Original Message----- From: JonD [mailto:jonathan.denniss at googlemail.com] Sent: Wednesday, August 27, 2008 3:47 PM To: r-help at r-project.org Subject: [R] averaging pairs of columns in a dataframe Dear all, I have a dataframe with 132 columns and 100 rows. Every 2nd column is a repeat measurement so that the columns could be titled, a a b b c c d d etc. I would like to average the repeats such that I am left with a data frame of 66 columns (of means) and 100 rows. I have been trying to use rowMeans but have not been able to average the pairs of columns, only the whole dataframe. Any help would be appreciated, I am new to R. Thanks in advance, Jon. -- View this message in context: http://www.nabble.com/averaging-pairs-of-columns-in-a-dataframe-tp19181319p1 9181319.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.