problem with break command

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with break command

cassie jones
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
    }
}

What exactly I am trying to do is find the sequence 'a' where the
individual values of 'a' would not be larger than 5. In any sequence, if it
becomes larger than 5, it would set the counter to be 2 and regenerate from
i=2. But I guess I have not put the break command in the right place, so
even though the 'if' condition becomes true and it sets i=2, it does not
regenerate the values of 'a' since the beginning. Any help is really
appreciated.

Thanks in advance.

Cassie

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem with break command

Berend Hasselman

On 26-04-2012, at 21:30, 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
>    }
> }
>
> What exactly I am trying to do is find the sequence 'a' where the
> individual values of 'a' would not be larger than 5. In any sequence, if it
> becomes larger than 5, it would set the counter to be 2 and regenerate from
> i=2. But I guess I have not put the break command in the right place, so
> even though the 'if' condition becomes true and it sets i=2, it does not
> regenerate the values of 'a' since the beginning. Any help is really
> appreciated.


break will exit the for loop. You cannot achieve what you desire with break.

You need a while loop for this.

i <- 2
while( i <= 5 )
# for(i in 2:5)
{
   a[i] <- a[i-1]+runif(1,0,3)
   if(a[i]>5) i <- 2 else i <- i+1
}

And hope that the while loop will eventually terminate.

Berend

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem with break command

cassie jones
Thanks Berend, you are right. The break command would not work here. But
the while loop is taking time to generate the desired.

On Thu, Apr 26, 2012 at 2:39 PM, Berend Hasselman <[hidden email]> wrote:

>
> On 26-04-2012, at 21:30, 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
> >    }
> > }
> >
> > What exactly I am trying to do is find the sequence 'a' where the
> > individual values of 'a' would not be larger than 5. In any sequence, if
> it
> > becomes larger than 5, it would set the counter to be 2 and regenerate
> from
> > i=2. But I guess I have not put the break command in the right place, so
> > even though the 'if' condition becomes true and it sets i=2, it does not
> > regenerate the values of 'a' since the beginning. Any help is really
> > appreciated.
>
>
> break will exit the for loop. You cannot achieve what you desire with
> break.
>
> You need a while loop for this.
>
> i <- 2
> while( i <= 5 )
> # for(i in 2:5)
> {
>   a[i] <- a[i-1]+runif(1,0,3)
>   if(a[i]>5) i <- 2 else i <- i+1
> }
>
> And hope that the while loop will eventually terminate.
>
> Berend
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem with break command

Petr Savicky
In reply to this post by cassie jones
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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem with break command

Petr Savicky
In reply to this post by cassie jones
On Thu, Apr 26, 2012 at 02:49:08PM -0500, cassie jones wrote:
> Thanks Berend, you are right. The break command would not work here. But
> the while loop is taking time to generate the desired.
>
> On Thu, Apr 26, 2012 at 2:39 PM, Berend Hasselman <[hidden email]> wrote:
[...]

> > You need a while loop for this.
> >
> > i <- 2
> > while( i <= 5 )
> > # for(i in 2:5)
> > {
> >   a[i] <- a[i-1]+runif(1,0,3)
> >   if(a[i]>5) i <- 2 else i <- i+1
> > }
> >
> > And hope that the while loop will eventually terminate.

Hi.

a[i] is the sum of (i-1) independent realizations of runif(1,0,3),
so a[5] has the mean 6 and standard deviation sqrt(3). The probability
that it is less than 5 may be estimated as

  x <- matrix(runif(4*100000, 0, 3), nrow=100000, ncol=4)
  mean(rowSums(x) < 5)
  [1] 0.28719

If you want to generate a larger number of instances, which terminate
below 5, try

  x <- matrix(runif(4*20, 0, 3), nrow=20, ncol=4)
  a <- matrix(0, nrow=4, ncol=5)
  a[col(a) > row(a)] <- 1
  y <- x[rowSums(x) < 5, ] %*% a

Petr Savicky.

______________________________________________
[hidden email] 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.