给定一个向量lookup
和一个有序向量ref
,我想要一个向量result
,其中result[i]
等于 ref
中最小元素的索引,使得 lookup[i] > ref[result[i]
例如:
ref <- c(1,5,10,15)
lookup <- c(2.3,10.5, 17,2,6)
result: 1,3,4,1,2
如何在不使用 for 循环的情况下在 R 中执行此操作?
最佳答案
由于 ref
向量是有序的,因此您可以使用 findInterval
来查找 ref
中的区间数字或索引> 对于数字 x:
ref <- c(1,5,10,15)
lookup <- c(2.3,10.5,17,2,6)
findInterval(lookup, ref)
# [1] 1 3 4 1 2
关于r - 查找大于列表中每个元素的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145082/