[R] Averaging across rows & columns

michael watson (IAH-C) michael.watson at bbsrc.ac.uk
Thu Jun 7 22:06:04 CEST 2007


Check out rowMeans to average over replicate columns first, ie:

means <- data.frame(t1=rowMeans(a[,1:3]), 
                    t2=rowMeans(a[,4:6]),
                    etc)

Then, if you want to aggregate every 14 rows:

aggregate(means, by=list(rows=rep(1:(nrow(means)/14), each=14)), mean)

Or something...

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch on behalf of Silvia Lomascolo
Sent: Thu 07/06/2007 8:26 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Averaging across rows & columns
 

I use Windows, R version 2.4.1.

I have a dataset in which columns 1-3 are replicates, 4-6, are replicates,
etc. I need to calculate an average for every set of replicates (columns
1-3, 4-6, 7-9, etc.) AND each set of replicates should be averaged every 14
rows (for more detail, to measure fruit color using a spectrometer, I
recorded three readings per fruit -replicates- that I need to average to get
one reading per fruit; each row is a point in the light spectrum and I need
to calculate an average reading every 5nm -14 rows- for each fruit).

Someone proposed to another user who wanted an avg across columns to do

a <- matrix(rnorm(360),nr=10)
b <- rep(1:12,each=3)
avgmat <- aggregate(a,by=list(b))

I tried doing this to get started with the columns first but it asks for an
argument FUN that has no default.  The help for aggregate isn't helping me
much (a new R user) to discover what value to give to FUN -'average' doesn't
seem to exist, and 'sum' (whatever it is supposed to sum) gives an error
saying that arguments should have the same length-

Any help will be much appreciated! 
Silvia.
-- 
View this message in context: http://www.nabble.com/Averaging-across-rows---columns-tf3885900.html#a11014649
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list