I'm subsetting a dataset before plotting, but the key being numeric I cannot use the strict equality testing of match()
or %in%
(it misses a few values).
I wrote the following alternative, but I imagine this problem is sufficiently common that there's a better built-in alternative somewhere? all.equal
doesn't seem to be designed for multiple test values.
select_in <- function(x, ref, tol=1e-10){
testone <- function(value) abs(x - value) < tol
as.logical(rowSums(sapply(ref, testone)) )
}
x = c(1.0, 1+1e-13, 1.01, 2, 2+1e-9, 2-1e-11)
x %in% c(1,2,3)
#[1] TRUE FALSE FALSE TRUE FALSE FALSE
select_in(x, c(1, 2, 3))
#[1] TRUE TRUE FALSE TRUE FALSE TRUE