[R] "which" is wrong?
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Mar 26 16:52:46 CET 2005
Troels Ring wrote:
> Dear friends,
> R 2.0.1 on windows XP:
>
> TOT <- seq(0.01,1,by=0.01)
> which(TOT==0.06)
> # numeric(0)
> which(TOT>0.06)
> # [1] 6 7 8 etc
> which(TOT<0.06)
> #[1] 1 2 3 4 5
> TOT[6]
> #[1] 0.06
> TOT[6]==0.06
> #[1] FALSE
>
> but
> TOT[5]==0.05
> #[1] TRUE
> and
> which(TOT==0.05)
> # [1] 5
>
>
>
>
>
> TOT looks as expected when printed,
> 0.35 and 0.36 behaves similarly
>
> I have tried on another machine with the same result - wonder what
> happens and whether
> this is as expected ?
It is expected: the 0.06 *calculated* by seq() is computationally a
little bit different than the 0.06 you have typed - we are working with
floating point calculations on a digital computer.
See ?all.equal for help.
Uwe Ligges
> Best wishes
> Troels Ring,
> Aalborg, Denmark
>
> ______________________________________________
> 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
More information about the R-help
mailing list