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

From: Bert Gunter <gunter.berton_at_gene.com>
Date: Wed, 18 Jun 2008 10:06:40 -0700

Folks:

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:

do.call(cbind,test)

etc.

Cheers,
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.

```

library(reshape)
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))

```--

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help