[R] computing the average and standard deviation for many setsof triplicates, using "R-approach"

Bert Gunter gunter.berton at gene.com
Wed Jun 18 19:06:40 CEST 2008


Assuming that you want row statistics of each block, it seems to me that
solutions so far proposed are either unnecessarily complex (a matter of
personal taste, I know) or depend unnecessarily on the specific arrangement
of the columns (with those beginning with same letter occurring together in
blocks). Since the column names are what determine the blocks over which
statistics are to be computed, a more straightforward approach would seem to
me to use them via grep():

 (note: "t" is a terrible name for your matrix, as it's already the name of
the R transpose function(which I use below). So I called your matrix "dat".)

cnm <- colnames(dat)
test <-lapply(LETTERS[1:4],function(l){
 	z <- dat[,grep(paste("^",l,sep=""),cnm)]
 	cbind(rowMeans(z), sd(t(z)))

"test" is now a list containing 4 matrices of 2 columns each with the
rowwise means and sd's. You can easily get into whatever form you like (or
leave it as is). For example, to make it into a single matrix:



Bert Gunter
Genentech, Inc.


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of hadley wickham
Sent: Wednesday, June 18, 2008 9:06 AM
To: Daren Tan
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] computing the average and standard deviation for many
setsof triplicates, using "R-approach"

On Wed, Jun 18, 2008 at 9:17 AM, Daren Tan <daren76 at hotmail.com> wrote:
> Below example has 4 sets of triplicates, without using for loop and
iteratively cbind the columns, what is the "R-approach" of generating a
matrix of 8 columns that are the averages and standard deviations ? The
average and standard deviation columns should be side by side i.e. A.mean
A.sd B.mean B.sd C.mean C.sd D.mean D.sd

t <- matrix(rnorm(120), ncol=12)
colnames(t) <- paste(rep(LETTERS[1:4], each=3), 1:3, sep=".")

# First get your data into a more useful format,
# where explanatory variables are explicitly encoded by
# columns in a data frame.

tm <- melt(t)
tm <- cbind(colsplit(tm$X2, "\\.", c("trt", "block")), tm)
tm$X2 <- NULL
tm <- rename(tm, c("X1" = "rep"))


# tm is now much easier to work with most R functions
# for your case, you can also use cast in the reshape package:

cast(tm, block ~ trt, c(mean, sd))
cast(tm, . ~ trt, c(mean, sd))

# for more info about the reshape package, see http://had.co.nz/reshape



R-help at r-project.org mailing list
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