[R] Logical Indexing of vectors -- Odd Behavior or....

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Mar 27 22:39:06 CET 2003


Joel Kincaid wrote:
> 
> R-Community,
> 
> I'm puzzled by the following behavior in R 1.6.2 and have found no
> reference to this in the archives:
> 
> >P <- seq(.1,.9,by=.1)
> 
> >P[P > .4]
> [1] 0.5 0.6 0.7 0.8 0.9
> as expected. However,
> >P[P > .3]
> [1] 0.3 0.4 0.5 0.6 0.7 0.8 0.9
>     ???
> Which is unexpected. Furthermore on the logical side
> > P>.1
> [1] FALSE TRUE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> > P>.2
> [1] FALSE FALSE TRUE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> > P>.3
> [1] FALSE FALSE TRUE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> > P>.4
> [1] FALSE FALSE FALSE FALSE TRUE TRUE  TRUE  TRUE  TRUE
> > P>.5
> [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE  TRUE  TRUE
> > P>.6
> [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
> > P>.7
> [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE  TRUE
> 
> What's with .3 and .7? Any pointers to where I might find out the info
> would be greatly appreciated,
> 
> I'm running 1.6.2 on windows XP,
> Sincerely,
> 
> Joel F. Kincaid, Ph. D.
> Assistant Professor
> Department of Economics and Finance
> Franklin P. Perdue School of Business
> Salisbury University
> Salisbury Maryland, 21801
> Phone: (410) 548-4416
> Email:   jfkincaid at salisbury.edu
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

a) It has nothing to do with indexing.
b) It not completely unexpected.

In seq() some computations are involved, hence it's just a very small
inaccuracy which is expected when working with limited machines such as
computers:

 options(digits=22) # some more details ...
 seq(.1, .9, .1)    # Ah, that's it! ;-)

Uwe Ligges



More information about the R-help mailing list