0

I have come across an odd thing I can't explain:

I am trying to find indices of elements from one vector in another vector, and I can do this with which() or match():

test = seq(.1, 10, .1)
match(c(0.1, 0.2, 4, 7), test)
[1]  1  2 40 70
which(test %in% c(0.1, 0.2, 4, 7))
[1]  1  2 40 70

This makes sense, but if I reverse the sequence:

test = seq(10, .1, -.1)
match(c(0.1, 0.2, 4, 7), test)
[1] 100  NA  61  31
which(test %in% c(0.1, 0.2, 4, 7))
[1]  31  61 100

it cannot find 0.2. My initial guess was that the format is not right, but it also cannot find 0.200 or test[99]

Can someone explain what is happening here?

LuleSa
  • 75
  • 7
  • 1
    It is a float object. So precision is important i.e. ` match(c(0.1, 0.2, 4, 7), round(test, 1))` – akrun Mar 03 '23 at 16:58
  • [`seq(.1, 10, .1) == rev(seq(10, .1, -.1))`](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – rawr Mar 03 '23 at 16:59
  • This is the usual issue: your descending sequence doesn't contain 0.2. See R FAQ 7.31. – user2554330 Mar 03 '23 at 16:59

0 Answers0