Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which.max ties method in R

Tags:

r

max

min

which.max and which.min will return the smallest index of the max or min value if there are ties.

Is there a way around this so that the largest index is returned without affecting the efficiency of the function?

max.col has this exact functionality, but I am dealing with a vector not a matrix.

like image 464
Omar Wagih Avatar asked Mar 26 '13 05:03

Omar Wagih


People also ask

WHAT DOES THE which Max function do in R?

which. max() function in R Language is used to return the location of the first maximum value in the Numeric Vector.

What is the min function in R?

which. min() function in R Language is used to return the location of the first minimum value in the Numeric Vector.


1 Answers

You could do like this:

x<-c(1,2,1,4,3,4)
#identical to which.max, except returns all indices with max
which(x==max(x)) 
[1] 4 6
z<-which(x==max(x))
z[length(z)]
[1] 6
#or with tail
tail(which(x==max(x)),1)
[1] 6

edit:

Or, you could also use max.col function for vectors like this:

max.col(t(x),"last")
[1] 6
#or
max.col(matrix(x,nrow=1),"last")
[1] 6

edit: Some benchmarking:

x<-sample(1:1000,size=10000,replace=TRUE)
library(microbenchmark)
microbenchmark(which.max(x),{z<-which(x==max(x));z[length(z)]}, 
     tail(which(x==max(x)),1),max.col(matrix(x,nrow=1),"last"),
     max.col(t(x),"last"),which.max(rev(x)),times=1000)
Unit: microseconds
                                             expr     min      lq  median      uq       max neval
                                     which.max(x)  29.390  30.323  30.323  31.256 17550.276  1000
 {     z <- which(x == max(x))     z[length(z)] }  40.586  42.452  42.919  44.318   631.178  1000
                      tail(which(x == max(x)), 1)  57.380  60.646  61.579  64.844   596.657  1000
             max.col(matrix(x, nrow = 1), "last") 134.353 138.085 139.485 144.383   710.949  1000
                            max.col(t(x), "last") 116.159 119.425 121.291 125.956   729.610  1000
                                which.max(rev(x))  89.569  91.435  92.368  96.566   746.404  1000

So all methods seem to be slower than the original (which gives wrong result), but z <- which(x == max(x));z[length(z)] seems to be fastest option of these.

like image 89
Jouni Helske Avatar answered Sep 18 '22 15:09

Jouni Helske