Is there a `match 'error in R?

How is this possible:

> match(1.68, seq(0.01,10, by = .01)) [1] 168 > match(1.67, seq(0.01,10, by = .01)) [1] NA 

Is there a match function in R?

+4
source share
2 answers

For this type of problem, I prefer the solution described by Chambers in his book "Data Analysis Software":

 match(1.68, seq(1, 1000, by = 1)/100) # [1] 168 match(1.67, seq(1, 1000, by = 1)/100) # [1] 167 

(This works because there is no floating point problem associated with creating a sequence of integers. Rounding occurs only when divided by 100 and corresponds to the rounding obtained by converting the entered number 1.67 to a binary file.)

This solution has the property of not finding a match for a number like 1.6744 , which is clearly not in the sequence 0.10, 0.11, 0.12, ..., 9.98, 9.99, 10.00 :

 match(1.6744, seq(1,1000, by = 1)/100) # [1] NA ## Just as I'd like it! 
+6
source

Typical R-FAQ 7.31 problem. Not a mistake. To avoid this common user error, use the findInterval function instead and reduce the boundaries slightly. (or make a selection by integer sequences.)

 > findInterval(1.69, seq(0.01,10, by = .01)) [1] 169 > findInterval(1.69, seq(0.01,10, by = .01)-.0001) [1] 169 > findInterval(1.68, seq(0.01,10, by = .01)-.0001) [1] 168 > findInterval(1.67, seq(0.01,10, by = .01)-.0001) [1] 167 > findInterval(1.66, seq(0.01,10, by = .01)-.0001) [1] 166 
+7
source

All Articles