在 R 中的排序向量中获取索引的最有效方法?
Most efficient way to get index in a sorted vector in R?
我有一个排序向量 x
,和一个包含限制的向量 L
(在没有特定假设的情况下按递增顺序),我想将所有限制的最大索引存储在 x
.我有一个解决方案,但它似乎效率低下。
执行此操作最快的方法是什么?
x = sort(runif(n = 1e6))
L = sort(runif(n = 100))
index = sapply(L, function(l) which.max(x>l))
您可以使用 findInterval
:
x = sort(runif(n = 1e6))
L = sort(runif(n = 100))
findInterval(L, x) + 1
我有一个排序向量 x
,和一个包含限制的向量 L
(在没有特定假设的情况下按递增顺序),我想将所有限制的最大索引存储在 x
.我有一个解决方案,但它似乎效率低下。
执行此操作最快的方法是什么?
x = sort(runif(n = 1e6))
L = sort(runif(n = 100))
index = sapply(L, function(l) which.max(x>l))
您可以使用 findInterval
:
x = sort(runif(n = 1e6))
L = sort(runif(n = 100))
findInterval(L, x) + 1