[R] Why does a[which(b == c[d])] not work?

Achim Zeileis zeileis at ci.tuwien.ac.at
Wed Oct 8 13:45:50 CEST 2003


On Wednesday 08 October 2003 11:27, Thomas Bock wrote:

> Dear list,
>
> I can not understand why the expression in
>
> the subject does not work correct:
>  > dcrn[which(fn == inve[2])]
>
> numeric(0)
>
>  > inve[2]
>
> [1] 406.7
>
>  > dcrn[which(fn == 406.7)]
>
>  [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07
> 1.3968e-07

The reason is that you shouldn't compare numeric output like that, 
consider the following example:

R> x <- 406.7 + 1e-5 
R> x
[1] 406.7
R> x == 406.7
[1] FALSE

whereas

R> x <- 406.7 + 1e-20 
R> x
[1] 406.7
R> x == 406.7
[1] TRUE

that is
  1.) `==' comparisons have a certain tolerance
  2.) the print output is not necessarily "precisely" your number

Instead of using `==' you should use a comparison with a certain 
tolerance you can specify...

hth,
Z


> Is this a kick self problem or an bug?
>
> Thaks very much
> Thomas
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list