[R] problem with break command

Petr Savicky savicky at cs.cas.cz
Thu Apr 26 21:56:34 CEST 2012


On Thu, Apr 26, 2012 at 02:30:09PM -0500, cassie jones wrote:
> Hello R-users,
> 
> I am having a problem with the 'break' command in R. I am wondering if
> anyone can help me out with this. My program is similar to the following.
> 
> a=rep(NA,5)
> a[1]=0
> for(i in 2:5)
> {
>     a[i]=a[i-1]+runif(1,0,3)
>     if(a[i]>5)
>     {
>         i=2
>         break
>     }
> }

Hi.

Berend sent you a solution.

Perhaps, one thing concerning the for loops in R should be
clarified. The variable specified as the loop variable is
not the true loop variable, which controls the loop, since
one can have a loop for (i in c(1, 1, 1, 1, 1)). The true
control variable is hidden and cannot be changed. So the
for loop

  for (i in 1:3) {
      for (i in 1:4) {
          cat("*")
      }
      cat("\n")
  }

works correctly although the visible loop variables are the
same.

Petr Savicky.



More information about the R-help mailing list