# [R] Help with loops

David Freedman 3.14david at gmail.com
Fri May 15 16:10:38 CEST 2009

```I'm not quite sure what you want to do, but this might help:

d=data.frame(replicate(40, rnorm(20)))
d\$sample=rep(c('a','b','c','d'),each=5)
lib(doBy)
summaryBy(.~sample,da=d)

David Freedman

Amit Patel-7 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
> 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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

--
View this message in context: http://www.nabble.com/Help-with-loops-tp23558647p23560599.html
Sent from the R help mailing list archive at Nabble.com.

```