Skip to content
Prev 305890 / 398502 Next

effective way to return only the first argument of "which()"

On 19-09-2012, at 20:02, Bert Gunter wrote:

            
And compiling the firsti function can also be quite lucrative!

firsti <- function(x,k)
{
    i <- 1
    while(x[i]<=k){i <- i+1}
    i
}

library(compiler)
firsti.c <- cmpfun(firsti)
[1] 93
[1] 93
user  system elapsed 
  0.014   0.000   0.013
user  system elapsed 
  0.002   0.000   0.002
user  system elapsed 
  2.148   0.013   2.164
user  system elapsed 
  0.393   0.002   0.396 

And in a new run (without the above tests)  with k=.999999 the index was 1089653 and the timing for the uncompiled function was 152 seconds and the timing for the compiled function was 28.8 seconds!

Berend