# Re: [R] missing value where TRUE/FALSE needed

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Wed 14 Jun 2006 - 16:56:26 EST

Hi

a<-1
b<-2
value<-2
if(value>=a&&value<=b) print("right") else print("wrong") [1] "right"
b<-NA
if(value>=a&&value<=b) print("right") else print("wrong") Error in if (value >= a && value <= b) print("right") else print("wrong") :

missing value where TRUE/FALSE needed

So one of your variables is NA when evaluating if statement and logical statement evaluates to NA but if expects true/false value.

Besides I wonder if you cannor get rid of for cycle.

>

On 13 Jun 2006 at 12:19, Peter Lauren wrote:

```Date sent:      	Tue, 13 Jun 2006 12:19:42 -0700 (PDT)
From:           	Peter Lauren <peterdlauren@yahoo.com>
To:             	r-help@stat.math.ethz.ch
Subject:        	[R] missing value where TRUE/FALSE needed

```

> I am using R 2.3.0 and trying to run the following
> code.
>
> BinCnt=round(ShorterVectorLength/10)
> BinMatrix=matrix(nrow=BinCnt, ncol=2)
> Increment=(VectorRange[2]-VectorRange[1])/BinCnt
> BinMatrix[1,1]=VectorRange[1]
> BinMatrix[1,2]=VectorRange[1]+Increment
> for (i in 2:BinCnt)
> {
> BinMatrix[i,1]=BinMatrix[i-1,2]
> BinMatrix[i,2]=BinMatrix[i,2]+Increment
> }
>
> # Count entries in each bin
> Histogram=vector(length=BinCnt, mode="numeric")
> for (i in 1:BinCnt) Histogram[i]=0
> VectorLength=length(FormerVector)
> for (i in 1:VectorLength)
> {
> Value=FormerVector[i]
> for (j in 1:BinCnt) if (Value>=BinMatrix[j,1] &&
> Value<=BinMatrix[j,2])
> {
> Histogram[j]=Histogram[j]+1
> break;
> }
> }
>
> Unfotunately, at
> if (Value>=BinMatrix[j,1] && Value<=BinMatrix[j,2])
> I get the following error message
> Error in if (Value >= BinMatrix[j, 1] && Value <=
> BinMatrix[j, 2]) { :
> missing value where TRUE/FALSE needed
> I inserted browser() just before the call and
> (Value>=BinMatrix[j,1] && Value<=BinMatrix[j,2])
> returned FALSE.
>
> Is there a bug in my code or a bug in my version of R?

Neither. The code does not suit your data.

HTH
Petr

>
> Peter.
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal@precheza.cz

R-help@stat.math.ethz.ch mailing list