Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a bug in `match' in R?

Tags:

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

Does the R function match have a bug in it?

like image 334
owensmartin Avatar asked Dec 13 '22 08:12

owensmartin


1 Answers

Typical R-FAQ 7.31 problem. Not a bug. To avoid this common user error, use instead the function findInterval and fuzz the boundaries down a bit. (or do your selections on 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
like image 173
IRTFM Avatar answered Dec 22 '22 01:12

IRTFM