0

I got this output in R, which I can't understand: I'm looking at whether the entries of the vector aa are present in the vector bb below. Could anybody explain me why is this that the entries of my output vector aren't all TRUE, as one would expect?

aa <- seq(0.1, 0.5, by = 0.1)
bb <- seq(0.01, 0.99, by = 0.01)
aa %in% bb
[1] FALSE  TRUE FALSE  TRUE  TRUE

Thank you

ixpl
  • 259
  • 1
  • 8
  • 4
    [10 problems, 1 solution.](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Ronak Shah Aug 23 '17 at 09:31
  • Ok I see.... Thanks for your quick reply! Well, then I guess the only way out is to loop over my two vectors using `all.equal` (?) This is a bit heavy... – ixpl Aug 23 '17 at 09:34
  • 1
    You don't need to loop, you could do something like this in one line: `sapply(seq_along(aa),function(x){any(abs(aa[x]-bb)<.Machine$double.eps^0.5)})` – Michael Bird Aug 23 '17 at 10:07
  • or just do something like `round(aa,2) %in% round(bb,2)` :) – Aramis7d Aug 23 '17 at 10:14

1 Answers1

-1

I would convert the vectors into a dataframe initiatlly, as it's easier to work with them as such.

library(dplyr)
aa <- seq(0.1, 0.5, by = 0.1)
bb <- seq(0.01, 0.99, by = 0.01)

aa <- as.data.frame(aa)
bb <- as.data.frame(bb)

aa <- mutate(aa, indicator =
        ifelse(aa %in% bb[, "bb"], 1, 0))
Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • 3
    This answer does not address the root of the problem: numerical precision. In addition, the suggestion that data.frames are easier to work with than raw vectors is at best a matter of taste. I personally prefer working with vectors in such situations. – lmo Aug 23 '17 at 12:12