Could you please provide help with something from R that I find rather puzzling? In the small program below x[1]=1, . . . , x[5]=5. R also finds that x[1]<=5 is TRUE. Yet when you attempt to execute while, R does not seem to recognize the condition. Any thoughts on why this happens? Regards Henri Moolman> x=c(1,2,3,4,5) > x[1][1] 1> i=1 > x[1]<=5[1] TRUE> while(x[i]<=5){+ i=i+1 + } Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed [[alternative HTML version deleted]]
The condition is true all the way until you index outside the vector... Cheers On 31 Mar 2018, 17.29 +0200, Henri Moolman <moolman.henri at gmail.com>, wrote:> Could you please provide help with something from R that I find rather > puzzling? In the small program below x[1]=1, . . . , x[5]=5. R also > finds that x[1]<=5 is TRUE. Yet when you attempt to execute while, R does > not seem to recognize the condition. Any thoughts on why this happens? > > Regards > > Henri Moolman > > > x=c(1,2,3,4,5) > > x[1] > [1] 1 > > i=1 > > x[1]<=5 > [1] TRUE > > while(x[i]<=5){ > + i=i+1 > + } > Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
When i increments to 6 (during the fifth iteration) the subsequent test of x[i]<=5 will produce an error since x has only five elements.> On 31 Mar 2018, at 14:45, Henri Moolman <moolman.henri at gmail.com> wrote: > > Could you please provide help with something from R that I find rather > puzzling? In the small program below x[1]=1, . . . , x[5]=5. R also > finds that x[1]<=5 is TRUE. Yet when you attempt to execute while, R does > not seem to recognize the condition. Any thoughts on why this happens? > > Regards > > Henri Moolman > >> x=c(1,2,3,4,5) >> x[1] > [1] 1 >> i=1 >> x[1]<=5 > [1] TRUE >> while(x[i]<=5){ > + i=i+1 > + } > Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hello, Maybe you want while(x[i] < 5) not < Hope this helps, Rui Barradas On 3/31/2018 2:45 PM, Henri Moolman wrote:> Could you please provide help with something from R that I find rather > puzzling? In the small program below x[1]=1, . . . , x[5]=5. R also > finds that x[1]<=5 is TRUE. Yet when you attempt to execute while, R does > not seem to recognize the condition. Any thoughts on why this happens? > > Regards > > Henri Moolman > >> x=c(1,2,3,4,5) >> x[1] > [1] 1 >> i=1 >> x[1]<=5 > [1] TRUE >> while(x[i]<=5){ > + i=i+1 > + } > Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
A. On Sat, 2018-03-31 at 15:45 +0200, Henri Moolman wrote:> Could you please provide help with something from R that I find rather > puzzling? In the small program below x[1]=1, . . . , x[5]=5. R also > finds that x[1]<=5 is TRUE. Yet when you attempt to execute while, R does > not seem to recognize the condition. Any thoughts on why this happens? > > Regards > > Henri Moolman > > > x=c(1,2,3,4,5) > > x[1] > [1] 1 > > i=1 > > x[1]<=5 > [1] TRUE > > while(x[i]<=5){ > + i=i+1 > + } > Error in while (x[i] <= 5) { : missing value where TRUE/FALSE neededIf you run the following you should understand why (the only change is to include "print(i)" in the loop, so you can see what is happening): x=c(1,2,3,4,5) x[1] # [1] 1 i=1 x[1]<=5 # [1] TRUE while(x[i]<=5){ i = i+1 ; print(i) } # [1] 3 # [1] 4 # [1] 5 # [1] 6 # Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed So everything is fine so long as i <= 5 (i.e. x[i] <= 5), but then the loop sets i = 6. and then: i # [1] 6 x[i] # [1] NA x[i] <= 5 # [1] NA Helpful? Best wishes, Ted.