[Rd] Match .3 in a sequence

Petr Savicky savicky at cs.cas.cz
Mon Mar 16 17:41:54 CET 2009


On Mon, Mar 16, 2009 at 06:36:53AM -0700, Daniel Murphy wrote:
> Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). I get
> > 0.3 %in% seq(from=.2,to=.3)
> [1] FALSE

As others already pointed out, you should use seq(from=0.2,to=0.3,by=0.1)
to get 0.3 in the sequence. In order to get correct %in%, it is also
possible to use round(), for example
  > 0.3 %in% round(seq(from=0.2,to=0.3,by=0.1),digits=1)
  [1] TRUE

See FAQ 7.31
  http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
or 
  http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy:decimal_numbers
for more detail.

Petr.



More information about the R-devel mailing list