[R] max.col weirdness

Ben Bolker bolker at ufl.edu
Fri May 29 00:03:58 CEST 2009




Daryl Morris wrote:
> 
> Hi,
> Sorry, I was too hasty.  I see that the tolerance is in the 
> documentation (and that "random" is the default tie-breaker).
> 
> Would it be possible to allow tolerance to be a parameter?
> 
> thanks, Daryl
> 
> 

  Could you just use apply(x,1,which.max) ?

> x <- matrix(c(112345.568,112345.569,112345.567),1) 
> x
         [,1]     [,2]     [,3]
[1,] 112345.6 112345.6 112345.6
> apply(x,1,which.max)
[1] 2

  It will presumably be less efficient than max.col (which
is implemented in C), and will automatically
pick the first value of the maximum if there are ties, but
should be OK for everyday use?

PS  oddly enough apply(x,1,which.max) is faster -- maybe because
it doesn't try to deal with ties?

> benchmark(max.col(x),apply(x,1,which.max),replications=50000)
                    test replications user.self sys.self elapsed user.child
2 apply(x, 1, which.max)        50000     7.649    0.456   8.110          0
1             max.col(x)        50000    11.185    0.580  11.828          0
  sys.child
2         0
1         0




-- 
View this message in context: http://www.nabble.com/max.col-weirdness-tp23769831p23770927.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list