0

A bizzarre behavior I just observed in R.

Started with:

d <- data.frame(x = rep(seq(1, 20, 0.2), 10))
subset(d, d$x == 17.4)

which returns nothing, even though 17.4 is clearly in the data frame. Same happens for 18.4 as well, and a few other values.

Same thing for:

x <- seq(1, 20, 0.2)
print(17.4 %in% x)

returns FALSE.

Checked on ideone.com, so no problem with my interpreter or anything.

Interestingly, if 17.4 is treated as a string, so do

"17.4" %in% x

that returns TRUE.

In this case x[83] is 17.4 and both str(x[83]) and str(17.4) are of course 'num', but:

x[83] == 17.4

returns FALSE.

I'm super baffled at what this small thing is about.

  • 1
    Seems like a case for [Why are these numbers not equal?](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Rich Scriven Oct 12 '15 at 02:27
  • @TheScientist5914: you should read the rest of the R-FAQ as well. – IRTFM Oct 12 '15 at 03:43
  • Great, thanks a lot! So the conclusion is that subsetting a data frame with a logical == is dangerous and shouldn't be done then? Quick search on Google shows me a number of places show such examples, which makes me uneasy given this example. – TheScientist5914 Oct 12 '15 at 04:16

0 Answers0