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