[R] missing value where TRUE/FALSE needed

Petr Pikal petr.pikal at precheza.cz
Wed Jun 14 08:56:26 CEST 2006


Hi

as nobody has any of your variables and can not reproduce your code 
you has to track your problems yourself. R is kindly provided you 
with hint for it.

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 at yahoo.com>
To:             	r-help at 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


> 
> Many thanks in advance,
> Peter.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list