[R] Help with loops

Amit Patel amitrhelp at yahoo.co.uk
Fri May 15 14:17:38 CEST 2009

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])


More information about the R-help mailing list