Grokbase Groups R r-help March 2002
FAQ
i am having some problems with a program that i am writing and i think that
knowing how the while command works will help me to figure out where i am
going wrong
*when do you get kicked out of a while loop?*
(i.e., in the following example what would the final value of 'a' be?) i
thought it would be 99 but running it in R i get 101 (and 101 for x too!)

while (x <100) {
for (i in 1:101) {
i -> x
x -> a
}
}

while this is a crude example, it is the basic form of what i am trying to
do, and i am guessing that while is not what i am looking for...

jimi adams
Department of Sociology
The Ohio State University
300 Bricker Hall
190 N Oval Mall
Columbus, OH 43210-1353
614-688-4261

our mind has a remarkable ability to think of contents as being independent
of the act of thinking
-georg simmel


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Search Discussions

  • Liaw, Andy at Mar 20, 2002 at 8:09 pm
    The for loop is nested inside the while loop, so it is finished before the
    next iteration of the while loop is attempted. So, in short, the for loop
    ran 101 times, ending with i, x, and a all equal to 101. Then the condition
    of the while loop is checked, (and not satisfied).

    Andy
    -----Original Message-----
    From: jimi adams [mailto:imij at columbus.rr.com]
    Sent: Wednesday, March 20, 2002 2:39 PM
    To: r-help at stat.math.ethz.ch
    Subject: [R] how does while work


    i am having some problems with a program that i am writing
    and i think that
    knowing how the while command works will help me to figure
    out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value
    of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and 101
    for x too!)

    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    }

    while this is a crude example, it is the basic form of what i
    am trying to
    do, and i am guessing that while is not what i am looking for...

    jimi adams
    Department of Sociology
    The Ohio State University
    300 Bricker Hall
    190 N Oval Mall
    Columbus, OH 43210-1353
    614-688-4261

    our mind has a remarkable ability to think of contents as
    being independent
    of the act of thinking
    -georg simmel
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To:
    r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._._._._._._._._
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Giovanni Petris at Mar 20, 2002 at 9:10 pm
    assuming you enter the while loop with a value of x less than 100,
    the for loop sets, in its last iteration, a=x1 (which is the last
    value `i' takes). at this point you also exit the while loop, since
    x<100 is false.

    Giovanni
    From: "jimi adams" <imij@columbus.rr.com>
    Date: Wed, 20 Mar 2002 14:39:13 -0500
    X-Priority: 3
    X-MSMail-Priority: Normal
    X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
    Sender: owner-r-help at stat.math.ethz.ch
    Precedence: SfS-bulk
    Content-Type: text/plain;
    charset="iso-8859-1"
    Content-Length: 1205

    i am having some problems with a program that i am writing and i think that
    knowing how the while command works will help me to figure out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and 101 for x too!)

    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    }

    while this is a crude example, it is the basic form of what i am trying to
    do, and i am guessing that while is not what i am looking for...

    jimi adams
    Department of Sociology
    The Ohio State University
    300 Bricker Hall
    190 N Oval Mall
    Columbus, OH 43210-1353
    614-688-4261

    our mind has a remarkable ability to think of contents as being independent
    of the act of thinking
    -georg simmel


    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
    --

    __________________________________________________
    [ ]
    [ Giovanni Petris GPetris at uark.edu ]
    [ Department of Mathematical Sciences ]
    [ University of Arkansas - Fayetteville, AR 72701 ]
    [ Ph: (479) 575-6324, 575-8630 (fax) ]
    [ http://definetti.uark.edu/~gpetris/ ]
    [__________________________________________________]

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Greg Snow at Mar 21, 2002 at 3:22 pm

    On Wed, 20 Mar 2002, jimi adams wrote:

    i am having some problems with a program that i am writing and i think that
    knowing how the while command works will help me to figure out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and 101 for x too!)

    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    }
    If you are trying to jump out of a for loop early then try something like:

    for (i in 1:101) {
    i -> x
    if (! x<100 ) { break }
    x -> a
    }

    the "next" command will also skip the rest of the body of the loop and
    start on the next iteration.

    Hope this helps,

    --
    Greg Snow, PhD Office: 223A TMCB
    Department of Statistics Phone: (801) 378-7049
    Brigham Young University Dept.: (801) 378-4505
    Provo, UT 84602 email: gls at byu.edu

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • David A Richmond at Mar 21, 2002 at 4:00 pm

    On Wed, 20 Mar 2002, jimi adams wrote:

    i am having some problems with a program that i am writing and i think that
    knowing how the while command works will help me to figure out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and 101 for x too!)

    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    *here*
    }
    Essentially the while condition doesn't get checked until just before the
    last curly brace (where I inserted *here* above), so the for loop gets to
    do it's thing until i (and x and a) reach 101 and only then is the while
    condition allowed to drop you out of the loop.
    while this is a crude example, it is the basic form of what i am trying to
    do, and i am guessing that while is not what i am looking for...
    what exactly are you trying to do?

    daver

    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    David Richmond It works on a |
    + Dept. of Sociology complex scientific +
    Saint Mary's College principle, known as |
    + Notre Dame, IN 46556 "pot luck." +
    574-284-4517 - The Doctor |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Liaw, Andy at Mar 21, 2002 at 5:55 pm
    What he wanted is a mechanism to "watch" for a condition on a variable, and
    stop execution as soon as that condition is TRUE, regardless of where in the
    program. The only thing that I know of that sort of does this is inside
    gdb, the GNU debugger. AFAIK this is impossible. If anyone has better
    idea, I'd like to hear about it, too.

    Andy
    -----Original Message-----
    From: David A Richmond [mailto:richmond at saintmarys.edu]
    Sent: Thursday, March 21, 2002 11:00 AM
    To: jimi adams
    Cc: r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work

    On Wed, 20 Mar 2002, jimi adams wrote:

    i am having some problems with a program that i am writing
    and i think that
    knowing how the while command works will help me to figure
    out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value
    of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and
    101 for x too!)
    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    *here*
    }
    Essentially the while condition doesn't get checked until
    just before the
    last curly brace (where I inserted *here* above), so the for
    loop gets to
    do it's thing until i (and x and a) reach 101 and only then
    is the while
    condition allowed to drop you out of the loop.
    while this is a crude example, it is the basic form of what
    i am trying to
    do, and i am guessing that while is not what i am looking for...
    what exactly are you trying to do?

    daver

    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    David Richmond It works on a |
    + Dept. of Sociology complex scientific +
    Saint Mary's College principle, known as |
    + Notre Dame, IN 46556 "pot luck." +
    574-284-4517 - The Doctor |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Giovanni Petris at Mar 21, 2002 at 8:16 pm
    Maybe warning() or stop() can be used for this purpose.

    Giovanni
    X-Server-Uuid: 7a42c1b6-0774-11d5-89f7-00508bcf402e
    Content-return: allowed
    Date: Thu, 21 Mar 2002 12:55:15 -0500
    From: "Liaw, Andy" <andy_liaw@merck.com>
    cc: r-help at stat.math.ethz.ch
    X-WSS-ID: 1084C110134707-08-01
    Sender: owner-r-help at stat.math.ethz.ch
    Precedence: SfS-bulk
    Content-Type: text/plain;
    charset=us-ascii
    Content-Length: 2731

    What he wanted is a mechanism to "watch" for a condition on a variable, and
    stop execution as soon as that condition is TRUE, regardless of where in the
    program. The only thing that I know of that sort of does this is inside
    gdb, the GNU debugger. AFAIK this is impossible. If anyone has better
    idea, I'd like to hear about it, too.

    Andy
    -----Original Message-----
    From: David A Richmond [mailto:richmond at saintmarys.edu]
    Sent: Thursday, March 21, 2002 11:00 AM
    To: jimi adams
    Cc: r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work

    On Wed, 20 Mar 2002, jimi adams wrote:

    i am having some problems with a program that i am writing
    and i think that
    knowing how the while command works will help me to figure
    out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value
    of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and
    101 for x too!)
    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    *here*
    }
    Essentially the while condition doesn't get checked until
    just before the
    last curly brace (where I inserted *here* above), so the for
    loop gets to
    do it's thing until i (and x and a) reach 101 and only then
    is the while
    condition allowed to drop you out of the loop.
    while this is a crude example, it is the basic form of what
    i am trying to
    do, and i am guessing that while is not what i am looking for...
    what exactly are you trying to do?

    daver

    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    David Richmond It works on a |
    + Dept. of Sociology complex scientific +
    Saint Mary's College principle, known as |
    + Notre Dame, IN 46556 "pot luck." +
    574-284-4517 - The Doctor |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
    --

    __________________________________________________
    [ ]
    [ Giovanni Petris GPetris at uark.edu ]
    [ Department of Mathematical Sciences ]
    [ University of Arkansas - Fayetteville, AR 72701 ]
    [ Ph: (479) 575-6324, 575-8630 (fax) ]
    [ http://definetti.uark.edu/~gpetris/ ]
    [__________________________________________________]

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Greg Snow at Mar 22, 2002 at 3:52 pm

    On Thu, 21 Mar 2002, Liaw, Andy wrote:

    What he wanted is a mechanism to "watch" for a condition on a variable, and
    stop execution as soon as that condition is TRUE, regardless of where in the
    program. The only thing that I know of that sort of does this is inside
    gdb, the GNU debugger. AFAIK this is impossible. If anyone has better
    idea, I'd like to hear about it, too.
    You can use "next" and "break" inside of a loop to change the flow (next
    goes on to the next iteration, break jumps completely out of the
    loop). Inside of a function you can use "return" to stop execution of the
    function early (look at the definition of mean.default, it uses return to
    stop processing if the trim is 0.5 or greater).

    using these with well designed loops should allow you to jump out anywhere
    that you need to.

    --
    Greg Snow, PhD Office: 223A TMCB
    Department of Statistics Phone: (801) 378-7049
    Brigham Young University Dept.: (801) 378-4505
    Provo, UT 84602 email: gls at byu.edu

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Liaw, Andy at Mar 21, 2002 at 8:59 pm
    Don't think so. stop() and warning() will only be triggered when explicited
    told to do so. Thus, essentially the (only?) idiotic way to do it is insert
    if(x>0) stop("x>100!") everywhere. There's no mechanism to keep track of
    a variable as the program executes, AFAIK.

    Andy
    -----Original Message-----
    From: Giovanni Petris [mailto:GPetris at uark.edu]
    Sent: Thursday, March 21, 2002 3:17 PM
    To: andy_liaw at merck.com
    Cc: richmond at saintmarys.edu; imij at columbus.rr.com;
    r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work



    Maybe warning() or stop() can be used for this purpose.

    Giovanni
    X-Server-Uuid: 7a42c1b6-0774-11d5-89f7-00508bcf402e
    Content-return: allowed
    Date: Thu, 21 Mar 2002 12:55:15 -0500
    From: "Liaw, Andy" <andy_liaw@merck.com>
    cc: r-help at stat.math.ethz.ch
    X-WSS-ID: 1084C110134707-08-01
    Sender: owner-r-help at stat.math.ethz.ch
    Precedence: SfS-bulk
    Content-Type: text/plain;
    charset=us-ascii
    Content-Length: 2731

    What he wanted is a mechanism to "watch" for a condition on
    a variable, and
    stop execution as soon as that condition is TRUE,
    regardless of where in the
    program. The only thing that I know of that sort of does
    this is inside
    gdb, the GNU debugger. AFAIK this is impossible. If
    anyone has better
    idea, I'd like to hear about it, too.

    Andy
    -----Original Message-----
    From: David A Richmond [mailto:richmond at saintmarys.edu]
    Sent: Thursday, March 21, 2002 11:00 AM
    To: jimi adams
    Cc: r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work

    On Wed, 20 Mar 2002, jimi adams wrote:

    i am having some problems with a program that i am writing
    and i think that
    knowing how the while command works will help me to figure
    out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value
    of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and
    101 for x too!)
    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    *here*
    }
    Essentially the while condition doesn't get checked until
    just before the
    last curly brace (where I inserted *here* above), so the for
    loop gets to
    do it's thing until i (and x and a) reach 101 and only then
    is the while
    condition allowed to drop you out of the loop.
    while this is a crude example, it is the basic form of what
    i am trying to
    do, and i am guessing that while is not what i am looking for...
    what exactly are you trying to do?

    daver

    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    David Richmond It works on a |
    + Dept. of Sociology complex scientific +
    Saint Mary's College principle, known as |
    + Notre Dame, IN 46556 "pot luck." +
    574-284-4517 - The Doctor |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To:
    r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._._._._._._.
    _._
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To:
    r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._._._._._._._._
    --

    __________________________________________________
    [ ]
    [ Giovanni Petris GPetris at uark.edu ]
    [ Department of Mathematical Sciences ]
    [ University of Arkansas - Fayetteville, AR 72701 ]
    [ Ph: (479) 575-6324, 575-8630 (fax) ]
    [ http://definetti.uark.edu/~gpetris/ ]
    [__________________________________________________]
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Mike Lonergan at Mar 22, 2002 at 10:10 am
    Andy,

    I agree that it would be idiotic to do this by hand, but another debug
    function that took the desired function, automatically added in the check
    after every completed command and ran the result might not be so daft.
    Though it would also need to add in extra {}s to avoid messing up single
    line ifs & fors.

    I assume there is a way of finding the end of statements (other than
    repeated try's)?

    Cheers,

    Mike.


    -----Original Message-----
    From: owner-r-help at stat.math.ethz.ch
    [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Liaw, Andy
    Sent: 21 March 2002 20:59
    To: 'Giovanni Petris'
    Cc: r-help at stat.math.ethz.ch
    Subject: RE: [R] how does while work
    >
    >
    Don't think so. stop() and warning() will only be triggered
    when explicited
    told to do so. Thus, essentially the (only?) idiotic way to
    do it is insert
    if(x>0) stop("x>100!") everywhere. There's no mechanism
    to keep track of
    a variable as the program executes, AFAIK. >
    Andy
    >
    -----Original Message-----
    From: Giovanni Petris [mailto:GPetris at uark.edu]
    Sent: Thursday, March 21, 2002 3:17 PM
    To: andy_liaw at merck.com
    Cc: richmond at saintmarys.edu; imij at columbus.rr.com;
    r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work
    > >
    > >
    > >
    Maybe warning() or stop() can be used for this purpose.
    > >
    Giovanni
    > >
    X-Server-Uuid: 7a42c1b6-0774-11d5-89f7-00508bcf402e
    Content-return: allowed
    Date: Thu, 21 Mar 2002 12:55:15 -0500
    From: "Liaw, Andy" <andy_liaw@merck.com>
    cc: r-help at stat.math.ethz.ch
    X-WSS-ID: 1084C110134707-08-01
    Sender: owner-r-help at stat.math.ethz.ch
    Precedence: SfS-bulk
    Content-Type: text/plain;
    charset=us-ascii
    Content-Length: 2731
    > > >
    What he wanted is a mechanism to "watch" for a condition on
    a variable, and
    stop execution as soon as that condition is TRUE,
    regardless of where in the
    program. The only thing that I know of that sort of does
    this is inside
    gdb, the GNU debugger. AFAIK this is impossible. If
    anyone has better
    idea, I'd like to hear about it, too.
    > > >
    Andy
    > > >
    -----Original Message-----
    From: David A Richmond [mailto:richmond at saintmarys.edu]
    Sent: Thursday, March 21, 2002 11:00 AM
    To: jimi adams
    Cc: r-help at stat.math.ethz.ch
    Subject: Re: [R] how does while work
    > > > >
    > > > >
    On Wed, 20 Mar 2002, jimi adams wrote:
    > > > >
    i am having some problems with a program that i am writing
    and i think that
    knowing how the while command works will help me to figure
    out where i am
    going wrong
    *when do you get kicked out of a while loop?*
    (i.e., in the following example what would the final value
    of 'a' be?) i
    thought it would be 99 but running it in R i get 101 (and
    101 for x too!)
    > > > > >
    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    }
    *here*
    }
    > > > >
    Essentially the while condition doesn't get checked until
    just before the
    last curly brace (where I inserted *here* above), so the for
    loop gets to
    do it's thing until i (and x and a) reach 101 and only then
    is the while
    condition allowed to drop you out of the loop.
    > > > >
    while this is a crude example, it is the basic form of what
    i am trying to
    do, and i am guessing that while is not what i am
    looking for...
    > > > >
    what exactly are you trying to do?
    > > > >
    daver
    > > > >
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    David Richmond It works on a |
    + Dept. of Sociology complex scientific +
    Saint Mary's College principle, known as |
    + Notre Dame, IN 46556 "pot luck." +
    574-284-4517 - The Doctor |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    > > > >
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To:
    r-help-request at stat.math.ethz.ch
    > > >
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._._._._._._.
    _._
    > > >
    > > >
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
    -.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To:
    r-help-request at stat.math.ethz.ch
    > > >
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._._._._._._._._
    > > >
    > >
    --
    > >
    __________________________________________________
    [ ]
    [ Giovanni Petris GPetris at uark.edu ]
    [ Department of Mathematical Sciences ]
    [ University of Arkansas - Fayetteville, AR 72701 ]
    [ Ph: (479) 575-6324, 575-8630 (fax) ]
    [ http://definetti.uark.edu/~gpetris/ ]
    [__________________________________________________]
    > >
    > >
    >
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    .-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read
    http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
    _._

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Jim Lemon at Mar 22, 2002 at 10:49 am
    This thread is fast becoming a citation classic. I thought it would have
    been resolved in a one-liner, but...

    What you really want to know is how Boolean logic works in stack-based
    computing. while() and for() evaluate their conditional(s) before
    executing the statement(s) that they are controlling. do() evaluates
    after execution. However, they all depend upon the same operator to
    delimit the statement(s) that they control (in R at least). Thus, you
    aren't allowed to do what jimi wants, to evaluate the while() condition
    before the for() condition like this:

    while (x <100) {
    for (i in 1:101) {
    i -> x
    x -> a
    } # end of while()
    } # end of for()

    What has to be done is something like this:

    for(i in 1:101) {
    x<-i
    if(x<100) a<-x
    }

    Now the conditional (x<100) takes precedence over the assignment, which
    was what was intended. Before anyone accuses me of preaching, I have
    made the same mistake (and worse) many times.

    Jim


    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 20, '02 at 7:39p
activeMar 22, '02 at 3:52p
posts11
users7
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase