[R] Help with a loop

Patrick Burns pburns at pburns.seanet.com
Mon Jul 28 10:52:09 CEST 2008


You are right to be dissatisfied with your code.

I suspect you will get more response if you say
what the code is supposed to do, and give a
smaller example of what the answer should be.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Sofia Martinez wrote:
> HI:
> I need ideas on how to make this code shorter (maybe with a second loop?).
> The code as it is works, but in this case I only have 14 samples, but it
> will become insane with more, so I need a way to make it more automatic. The
> problem is that the output from ts1, ts2, and so on is a vector with more
> than one value, so I do not know how to solve this.
>    Thanks
>       Prenewbie
>
> The code is the following:
>
> duration<-c(750, 8940,17180, 8693,10100, 7990,24820, 6770,
> 7390,8450,18400,16252,6080,11030)
>
> tmax<-0
> dt<-0
> for (m in 1:14)
>  {
>   tmax[m]<-duration[m]
>    dtmin<-969
>    dtmax<-9884
>    i<-1
>    dt[m]<-round(runif(1,dtmin,(tmax[m]-1)))}
>       ts<-c(1)
>       ts1<-ts
>       ts2<-ts1
>       ts3<-ts1
>       ts4<-ts1
>       ts5<-ts1
>       ts6<-ts1
>       ts7<-ts1
>       ts8<-ts1
>       ts9<-ts1
>       ts10<-ts1
>       ts11<-ts1
>       ts12<-ts1
>       ts13<-ts1
>       ts14<-ts1
>       for (m in 1:2){
>       while(i+dt[m]<tmax[m])
>       {
>       ts1<-append(ts,i+dt[1])
>       i<-i+dt[1]
>       ifelse(dtmin<=(tmax[1]-i), dt[1]<-round(runif(1,dtmin,(tmax[1]-i))),
> dt[1]<-(tmax[1]-i))
>       ts2<-append(ts,i+dt[2])
>       i<-i+dt[2]
>       ifelse(dtmin<=(tmax[2]-i), dt[2]<-round(runif(1,dtmin,(tmax[2]-i))),
> dt[2]<-(tmax[2]-i))
>        ts3<-append(ts,i+dt[3])
>       i<-i+dt[3]
>       ifelse(dtmin<=(tmax[3]-i), dt[3]<-round(runif(1,dtmin,(tmax[3]-i))),
> dt[3]<-(tmax[3]-i))
>        ts4<-append(ts,i+dt[4])
>       i<-i+dt[4]
>       ifelse(dtmin<=(tmax[4]-i), dt[4]<-round(runif(1,dtmin,(tmax[4]-i))),
> dt[4]<-(tmax[4]-i))
>        ts5<-append(ts,i+dt[5])
>       i<-i+dt[5]
>       ifelse(dtmin<=(tmax[5]-i), dt[5]<-round(runif(1,dtmin,(tmax[5]-i))),
> dt[5]<-(tmax[5]-i))
>        ts6<-append(ts,i+dt[6])
>       i<-i+dt[6]
>       ifelse(dtmin<=(tmax[6]-i), dt[6]<-round(runif(1,dtmin,(tmax[6]-i))),
> dt[6]<-(tmax[6]-i))
>        ts7<-append(ts,i+dt[7])
>       i<-i+dt[7]
>       ifelse(dtmin<=(tmax[7]-i), dt[7]<-round(runif(1,dtmin,(tmax[7]-i))),
> dt[7]<-(tmax[7]-i))
>        ts8<-append(ts,i+dt[8])
>       i<-i+dt[8]
>       ifelse(dtmin<=(tmax[8]-i), dt[8]<-round(runif(1,dtmin,(tmax[8]-i))),
> dt[8]<-(tmax[8]-i))
>        ts9<-append(ts,i+dt[9])
>       i<-i+dt[9]
>       ifelse(dtmin<=(tmax[9]-i), dt[9]<-round(runif(1,dtmin,(tmax[9]-i))),
> dt[9]<-(tmax[9]-i))
>        ts10<-append(ts,i+dt[10])
>       i<-i+dt[10]
>       ifelse(dtmin<=(tmax[10]-i),
> dt[10]<-round(runif(1,dtmin,(tmax[10]-i))), dt[10]<-(tmax[10]-i))
>        ts11<-append(ts,i+dt[11])
>       i<-i+dt[11]
>       ifelse(dtmin<=(tmax[11]-i),
> dt[11]<-round(runif(1,dtmin,(tmax[11]-i))), dt[11]<-(tmax[11]-i))
>        ts12<-append(ts,i+dt[12])
>       i<-i+dt[12]
>       ifelse(dtmin<=(tmax[12]-i),
> dt[12]<-round(runif(1,dtmin,(tmax[12]-i))), dt[12]<-(tmax[12]-i))
>        ts13<-append(ts,i+dt[13])
>       i<-i+dt[13]
>       ifelse(dtmin<=(tmax[13]-i),
> dt[13]<-round(runif(1,dtmin,(tmax[13]-i))), dt[13]<-(tmax[13]-i))
>        ts14<-append(ts,i+dt[14])
>       i<-i+dt[14]
>       ifelse(dtmin<=(tmax[14]-i),
> dt[14]<-round(runif(1,dtmin,(tmax[14]-i))), dt[14]<-(tmax[14]-i))
>       } }
>      ts1<-append(ts,i+dt[1])
>      ts2<-append(ts,i+dt[2])
>      ts3<-append(ts,i+dt[3])
>      ts4<-append(ts,i+dt[4])
>      ts5<-append(ts,i+dt[5])
>      ts6<-append(ts,i+dt[6])
>      ts7<-append(ts,i+dt[7])
>      ts8<-append(ts,i+dt[8])
>      ts9<-append(ts,i+dt[9])
>      ts10<-append(ts,i+dt[10])
>      ts11<-append(ts,i+dt[11])
>      ts12<-append(ts,i+dt[12])
>      ts13<-append(ts,i+dt[13])
>      ts14<-append(ts,i+dt[14])
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
>
>



More information about the R-help mailing list