Fastest way to find *the index* of the second (third…) highest/lowest value in vector or column

后端 未结 7 905
青春惊慌失措
青春惊慌失措 2020-12-17 10:04

Fastest way to find the index of the second (third...) highest/lowest value in vector or column ?

i.e. what

sort(x,partial=n-1)[n-1]
         


        
相关标签:
7条回答
  • 2020-12-17 10:59

    EDIT 2 :

    As Joshua pointed out, none of the given solutions actually performs correct when you have a tie on the maxima, so :

    X <- c(11:19,19)
    
    n <- length(unique(X))
    which(X == sort(unique(X),partial=n-1)[n-1])
    

    fastest way of doing it correctly then. I deleted the order way, as that one doesn't work and is a lot slower, so not a good answer according to OP.

    To point to the issue we ran into :

    > X <- c(11:19,19)    
    > n <- length(X)
    > which(X == sort(X,partial=n-1)[n-1])
    [1]  9 10 #which is the indices of the double maximum 19
    
    > n <- length(unique(X))
    > which(X == sort(unique(X),partial=n-1)[n-1])
    [1] 8 # which is the correct index of 18
    

    The timings of the valid solutions :

    > x <- runif(1000000)
    
    > ind <- 2
    
    > n <- length(unique(x))
    
    > system.time(which(x == sort(unique(x),partial=n-ind+1)[n-ind+1]))
       user  system elapsed 
       0.11    0.00    0.11 
    
    > system.time(sapply(sort(unique(x), index.return=TRUE), `[`, n-ind+1))
       user  system elapsed 
       0.69    0.00    0.69 
    
    0 讨论(0)
提交回复
热议问题