Dear Amit,
The following should get you started:

# Some data
set.seed(123)
X <- matrix(rnorm(20*10), ncol=10)
X

# Group of replicates
g <- rep(1:(ncol(X)/2), each=2)
g

# Mean of replicate variables
t(apply(X, 1, tapply, g, mean, na.rm = TRUE))

I created a grouping variable (g) and then calculate the mean by row (
apply(X, 1,...) ) for each level of g (that's why I included tapply).

I have not checked timing but I guess it is faster than the script you
already have.

HTH,

Jorge



On Mon, Jun 8, 2009 at 11:45 AM, Amit Patel <amitrhelp@yahoo.co.uk> wrote:

>
> Hi
> i am using a script which involves the following loop. It attempts to
> reduce a data frame(zz) of 95000 * 41 down to a data frame
> (averagedreplicates) of 95000 * 21 by averaging the replicate values as you
> can see in the script below. This script however is very slow (2days). Any
> suggestions to speed it up.
>
> NB I have also tried using rowMeans rather than adding the 2 values and
> dividing by 2. (same problem)
>
>
>
>
> #SCRIPT STARTS
> for (i in 1:length(averagedreplicates[,1]))
> #for (i in 1:dim(averagedreplicates)[1])
> {
> cat(i,'\n')
>
>
> #calculates Meanss
> #Sample A
> averagedreplicates[i,2] <- (zz[i,2] + zz[i,3])/2
> averagedreplicates[i,3] <- (zz[i,4] + zz[i,5])/2
> averagedreplicates[i,4] <- (zz[i,6] + zz[i,7])/2
> averagedreplicates[i,5] <- (zz[i,8] + zz[i,9])/2
> averagedreplicates[i,6] <- (zz[i,10] + zz[i,11])/2
>
> #Sample B
> averagedreplicates[i,7] <- (zz[i,12] + zz[i,13])/2
> averagedreplicates[i,8] <- (zz[i,14] + zz[i,15])/2
> averagedreplicates[i,9] <- (zz[i,16] + zz[i,17])/2
> averagedreplicates[i,10] <- (zz[i,18] + zz[i,19])/2
> averagedreplicates[i,11] <- (zz[i,20] + zz[i,21])/2
>
> #Sample C
> averagedreplicates[i,12] <- (zz[i,22] + zz[i,23])/2
> averagedreplicates[i,13] <- (zz[i,24] + zz[i,25])/2
> averagedreplicates[i,14] <- (zz[i,26] + zz[i,27])/2
> averagedreplicates[i,15] <- (zz[i,28] + zz[i,29])/2
> averagedreplicates[i,16] <- (zz[i,30] + zz[i,31])/2
>
> #Sample D
> averagedreplicates[i,17] <- (zz[i,32] + zz[i,33])/2
> averagedreplicates[i,18] <- (zz[i,34] + zz[i,35])/2
> averagedreplicates[i,19] <- (zz[i,36] + zz[i,37])/2
> averagedreplicates[i,20] <- (zz[i,38] + zz[i,39])/2
> averagedreplicates[i,21] <- (zz[i,40] + zz[i,41])/2
>  }
>
>
>
> ______________________________________________
> 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]]


