Hi R users, is there any possibilty that a while loop is working like that: z <- c(0,1,2,3,4,5,6,7,8,9) r <- 7 while(w == T) { for ( i in 1:10 ){ w <- r == z[i] print(w) } } The loop should stop if w == TRUE best regards -- View this message in context: http://r.789695.n4.nabble.com/While-loop-working-with-TRUE-FALSE-tp4348340p4348340.html Sent from the R help mailing list archive at Nabble.com.
You are combining too many loop constructs: perhaps you just want to use for and break. Of course, in your case it's much faster to write which(r == z) or which.min(r == z) Michael On Wed, Feb 1, 2012 at 10:55 AM, Chris82 <rubenbauar at gmx.de> wrote:> Hi R users, > > is there any possibilty that a while loop is working like that: > > z <- c(0,1,2,3,4,5,6,7,8,9) > r <- 7 > > ? while(w == T) { > ? ? ? ?for ( i in 1:10 ){ > ? ? ? ? ? ? ? ?w <- r == z[i] > ? ? ? ? ? ? ? ?print(w) > ? ? ? ?} > } > > > The loop should stop if w == TRUE > > > best regards > > -- > View this message in context: http://r.789695.n4.nabble.com/While-loop-working-with-TRUE-FALSE-tp4348340p4348340.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Le mercredi 01 f?vrier 2012 ? 07:55 -0800, Chris82 a ?crit :> Hi R users, > > is there any possibilty that a while loop is working like that: > > z <- c(0,1,2,3,4,5,6,7,8,9) > r <- 7 > > while(w == T) { > for ( i in 1:10 ){ > w <- r == z[i] > print(w) > } > }What problem are you trying to solve? In R, loops should generally be avoided, and you have much simpler syntax for most cases.> The loop should stop if w == TRUEYour while loop stops when w == FALSE here. Anyway, it won't run since w isn't set the first time the condition is tested. And I really don't see why you have two embedded loops here. Cheers
On 01-02-2012, at 16:55, Chris82 wrote:> Hi R users, > > is there any possibilty that a while loop is working like that: > > z <- c(0,1,2,3,4,5,6,7,8,9) > r <- 7 > > while(w == T) { > for ( i in 1:10 ){ > w <- r == z[i] > print(w) > } > } > > > The loop should stop if w == TRUE1. This won't work since the variable w doesn't exist at the start of the while loop. 2. The while loop condition is incorrect: it should be while( w == FALSE) 3. Don't use T and F. Use TRUE and FALSE. 4. If there is no element of z equal to r the loop will run forever (with the correct condition of course). A much better way of doing what you seem to be wanting is which(z == r) or possibly any(z == r) Berend
Thanks to both. This is just a simple example. In real I have two vectors with different lengths. The code consists of two for loops for r and z. The main problem is the computational time, so I try to stop the loop if w is TRUE for the first time. First I tried to use the command "stop" in combination with the two for loops, but then my whole script stoped. The script not only consists of this stuff. -- View this message in context: http://r.789695.n4.nabble.com/While-loop-working-with-TRUE-FALSE-tp4348340p4348490.html Sent from the R help mailing list archive at Nabble.com.
No. The while loop is only tested after the for loop has completed. Use debug to understand this if it doesn't make sense to you. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Chris82 <rubenbauar at gmx.de> wrote:>Hi R users, > >is there any possibilty that a while loop is working like that: > >z <- c(0,1,2,3,4,5,6,7,8,9) >r <- 7 > > while(w == T) { > for ( i in 1:10 ){ > w <- r == z[i] > print(w) > } >} > > >The loop should stop if w == TRUE > > >best regards > >-- >View this message in context: >http://r.789695.n4.nabble.com/While-loop-working-with-TRUE-FALSE-tp4348340p4348340.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
On 01-02-2012, at 17:32, Chris82 wrote:> Thanks to both. > > This is just a simple example. In real I have two vectors with different > lengths. > The code consists of two for loops for r and z. The main problem is the > computational time, so I try to stop the loop if w is TRUE for the first > time. >Maybe this helps: z <- c(0,1,2,3,4,5,6,7,8,9) r <- c(3,7) fun1 <- function(r, z) sapply(r,function(x) which(x == z)) fun1(r,z) Berend