0

I want to use findInterval function but it acts odd for numbers on the border. Here is the anomaly I observed.

bins <- seq(from = -4.4, to = 1.2, by = .8); bins
[1] -4.4 -3.6 -2.8 -2.0 -1.2 -0.4  0.4  1.2
findInterval(x = -.4, vec = bins)
[1] 6
findInterval(x = .4, vec = bins)
[1] 6

bins <- seq(from = -3.6, to = 1.2, by = .8); bins
[1] -3.6 -2.8 -2.0 -1.2 -0.4  0.4  1.2
findInterval(x = -.4, vec = bins)
[1] 4
findInterval(x = .4, vec = bins)
[1] 6

bins <- seq(from = -2.8, to = 1.2, by = .8); bins
[1] -2.8 -2.0 -1.2 -0.4  0.4  1.2
findInterval(x = -.4, vec = bins)
[1] 3
findInterval(x = .4, vec = bins)
[1] 4

I expected an output like the last one. Am I missing something here? Why this function puts consecutive numbers in such a random manner?

HBat
  • 4,873
  • 4
  • 39
  • 56
  • 4
    Almost certainly due to floating point rounding issues. See what you get if you remove the decimal points (even the third example changes). – Henry Sep 06 '14 at 19:11
  • 1
    http://www.hep.by/gnu/r-patched/r-faq/R-FAQ_82.html . `seq()` is a common cause of these kinds of questions. – Ben Bolker Sep 06 '14 at 19:15
  • 1
    @BenBolker makes an excellent point about `seq` often causing problem. It's often better to do `bins <- seq(from = -44, to = 12, by = 8)/10; bins` and `bins <- seq(from = -36, to = 12, by = 8)/10; bins` for example. Of if you're treating them like discrete values, move them to an integer scale for good. – MrFlick Sep 06 '14 at 19:25

0 Answers0