2

I am having trouble understanding the output of the function unique(). If I try to reduce the following vector to non-duplicated values I get:

unique(c(1*1e-04,10*1e-05))
[1] 1e-04

But if I try with two values one tenth smaller I get:

unique(c(1*1e-05,10*1e-06))
[1] 1e-05 1e-05

What is going on here? Is there a way to get only one value in the second example?

Jinglestar
  • 376
  • 1
  • 10
  • 3
    This is round-off error. see this post on [floating point Arithmetic](https://stackoverflow.com/q/588004/4752675) – G5W Sep 13 '17 at 15:19
  • Thank you for pointing this out. Still, as for my second question, is there an easy way to make the function consider the two values as being identical? – Jinglestar Sep 13 '17 at 15:34
  • 1
    In the link "why are these numbers not equal?" there are some workarounds. For pairwise comparisons, the standard is `abs(a - b) < tol`; but for uniquifying a vector, you may have to choose your own approach, like maybe `unique(round(x*1e6))/1e6` or something, since I don't think there's a standard. – Frank Sep 13 '17 at 15:41

0 Answers0