问题
I have an ordered vector of unique integers in R and I want to find the index of the element closest to but less than or equal to some value. For example, for the vector 4 8 15 16 23 42
and the search value 17
, I would like the function to return 4
, the index of 16
. In Python, I would use
bisect module. Is there anything similar in R?
回答1:
You can use binsearch
from the gtools
package to get the log(n) behavior of a binary search:
library(gtools)
x <- c(4, 8, 15, 16, 23, 42)
binsearch(function(y) x[y]-17, range=c(1, length(x)))
# $call
# binsearch(fun = function(y) x[y] - 17, range = c(1, length(x)))
#
# $numiter
# [1] 3
#
# $flag
# [1] "Between Elements"
#
# $where
# [1] 4 5
#
# $value
# [1] -1 6
We can see the sublinear scaling of the number of iterations with a larger example:
set.seed(144)
x <- sort(runif(1000000))
binsearch(function(y) x[y]-0.5, range=c(1, length(x)))
# $call
# binsearch(fun = function(y) x[y] - 0.5, range = c(1, length(x)))
#
# $numiter
# [1] 21
#
# $flag
# [1] "Between Elements"
#
# $where
# [1] 500577 500578
#
# $value
# [1] -1.990702e-07 5.827751e-07
回答2:
Base R provides findInterval which implements a binary search:
findInterval(17, c(4, 8, 15, 16, 23, 42))
@Khashaa already mentioned this in a comment.
来源:https://stackoverflow.com/questions/31526464/find-index-of-value-in-a-sorted-vector-in-r