<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/thou-shalt-not-compare-numeric-values-except-when-it-works/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jcarroll.com.au/2017/09/04/thou-shalt-not-compare-numeric-values-except-when-it-works/"> R – Irregularly Scheduled Programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This was just going to be a few Tweets but it ended up being a bit of a rollercoaster of learning for me, and I haven’t blogged in far too long, so I’m writing it up quickly as a ‘hey look at that’ example for newcomers.</p>
<p><span></span></p>
<p>I’ve been working on the ‘merging data’ part of <a rel="nofollow" href="https://www.manning.com/books/data-munging-with-r" target="_blank">my book</a> and, as I do when I’m writing this stuff, I had a play around with some examples to see if there was anything funky going on if a reader was to try something slightly different. I’ve been using <code>dplyr</code> for the examples after being thoroughly convinced on Twitter to do so. It’s going well. Mostly.</p>
<pre>## if you haven't already done so, load dplyr
suppressPackageStartupMessages(library(dplyr))</pre>
<p>My example involved joining together two <code>tibble</code>s containing text values. Nothing too surprising. I wondered though; do numbers behave the way I expect?</p>
<p>Now, a big rule in programming is ‘thou shalt not compare numbers’, and it holds especially true <a rel="nofollow" href="http://0.30000000000000004.com/" target="_blank">when numbers aren’t exactly integers</a>. This is because representing non-integers is hard, and what you see on the screen isn’t always what the computer sees internally.</p>
<p>
<div class="wp-caption"><a rel="nofollow" href="https://i0.wp.com/jcarroll.com.au/wp-content/uploads/2017/09/AngryGod-300x188.jpg?ssl=1" target="_blank"><img alt="Thou shalt not compare numbers." src="https://i0.wp.com/jcarroll.com.au/wp-content/uploads/2017/09/AngryGod.jpg?resize=680%2C485&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Thou shalt not compare numbers.</p>
</div>
<p>If I had a <code>tibble</code> where the column I would use to <code>join</code> had integers</p>
<pre>dataA &lt;- tribble(
    ~X, ~Y,
    0L, 100L,
    1L, 101L,
    2L, 102L,
    3L, 103L
)
dataA</pre>
<pre>## # A tibble: 4 x 2
##       X     Y
##   </pre>