Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:条件为vector时的哪个()_R - Fatal编程技术网

R:条件为vector时的哪个()

R:条件为vector时的哪个(),r,R,我有数据 test <- 1:10 屈服 当单独执行时,是否有一种方法可以使用类似于 bounds <- c(5,8,9) bounds只需使用apply: sapply(边界,函数(x),其中(test>x)[1]) [1] 6 9 10提供了几个选项 findInterval(bounds, test) + 1 #[1] 6 9 10 哪一个最快 max.col(outer(bounds, test, `<`), 'first') #[1] 6 9 10 它

我有数据

test <- 1:10
屈服

当单独执行时,是否有一种方法可以使用类似于

bounds <- c(5,8,9)
bounds只需使用apply:

sapply(边界,函数(x),其中(test>x)[1])

[1] 6 9 10

提供了几个选项

findInterval(bounds, test) + 1
#[1]  6  9 10
哪一个最快

max.col(outer(bounds, test, `<`), 'first')
#[1]  6  9 10

它既不是最快的,也不是最慢的。

一个选项是
sapply(边界,函数(x)它(test>x)[1])#[1]6 9 10
谢谢-事实证明,
findInterval()
是最快的,
max.col()
需要大约1000倍的时间
sapply()
大约100次。干杯你能将后者添加到答案中吗?@bumblebee如果你有关于大数据的基准,请继续编辑答案
findInterval(bounds, test) + 1
#[1]  6  9 10
max.col(outer(bounds, test, `<`), 'first')
#[1]  6  9 10
sapply(bounds, function(x) which(test > x)[1])
#[1] 6 9 10