[R] help to speed up loops in r

Amit Patel amitrhelp at yahoo.co.uk
Mon Jun 8 17:45:40 CEST 2009


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
  }


   



More information about the R-help mailing list