Dear Amit,
The following should get you started. What I'm doing is creating an
identifiers (g) with the "names" of the columns you want to group for and
then use a combination of apply() and tapply() to get the mean for each row
in the levels of g. In your case, you have more columns than I have in my
example, but with slightly modifications you can adapt the code below to
your needs.

See ?apply and ?rep for more information.

HTH,

Jorge


# Some data
set.seed(123)
X <- matrix(rnorm(100), ncol=10)
colnames(X) <- paste('x',1:10,sep="")
rownames(X) <- paste('sample_',1:10,sep="")

# Defining the groups using seq()
g <- rep(1:(ncol(X)/2), each = 2 )

# Calculating the means
res <- t( apply(X, 1, tapply, g, mean) )
res

# res[1,1] is the mean for X[1, 1:2]
mean(X[1,1:2])
# [1] 0.2408457


On Fri, May 15, 2009 at 8:17 AM, Amit Patel <amitrhelp@yahoo.co.uk> wrote:

>
> Hi
> I am trying to create a loop which averages replicates in my data.
> The original data has many rows. and consists of 40 column zz[,2:41] plus
> row headings in zz[,1]
> I am trying to average each set of values (i.e. zz[1,2:3] averaged and
> placed in average_value[1,2] and so on.
> below is my script but it seems to be stuck in an endless loop
> Any suggestions??
>
> for (i in 1:length(average_value[,1])) {
> average_value[i] <- i^100; print(average_value[i])
>
> #calculates Meanss
> #Sample A
> average_value[i,2] <- rowMeans(zz[i,2:3])
> average_value[i,3] <- rowMeans(zz[i,4:5])
> average_value[i,4] <- rowMeans(zz[i,6:7])
> average_value[i,5] <- rowMeans(zz[i,8:9])
> average_value[i,6] <- rowMeans(zz[i,10:11])
>
> #Sample B
> average_value[i,7] <- rowMeans(zz[i,12:13])
> average_value[i,8] <- rowMeans(zz[i,14:15])
> average_value[i,9] <- rowMeans(zz[i,16:17])
> average_value[i,10] <- rowMeans(zz[i,18:19])
> average_value[i,11] <- rowMeans(zz[i,20:21])
>
> #Sample C
> average_value[i,12] <- rowMeans(zz[i,22:23])
> average_value[i,13] <- rowMeans(zz[i,24:25])
> average_value[i,14] <- rowMeans(zz[i,26:27])
> average_value[i,15] <- rowMeans(zz[i,28:29])
> average_value[i,16] <- rowMeans(zz[i,30:31])
>
> #Sample D
> average_value[i,17] <- rowMeans(zz[i,32:33])
> average_value[i,18] <- rowMeans(zz[i,34:35])
> average_value[i,19] <- rowMeans(zz[i,36:37])
> average_value[i,20] <- rowMeans(zz[i,38:39])
> average_value[i,21] <- rowMeans(zz[i,40:41])
>  }
>
>
> thanks
>
>
>
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]


