# [R] R help

Ted Harding ted.harding at wlandres.net
Sat Mar 31 17:54:05 CEST 2018

```     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, .  .  .  , x=5. R also
> finds that x<=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
> > i=1
> > x<=5
>  TRUE
> > while(x[i]<=5){
> +     i=i+1
> + }
> Error in while (x[i] <= 5) { : missing value where TRUE/FALSE needed

If 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
i=1
x<=5
#  TRUE
while(x[i]<=5){
i = i+1 ; print(i)
}
#  3
#  4
#  5
#  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
#  6
x[i]
#  NA
x[i] <= 5
#  NA