2013-03-26 10 views
12

which.max e which.min restituirà l'indice più piccolo del valore massimo o minimo se ci sono cravatte.which.max cravatta il metodo in R

Esiste un modo per aggirare questo in modo che venga restituito l'indice più grande senza che influisce sull'efficienza della funzione?

max.col ha questa funzionalità esatta, ma mi occupo di un vettore non di una matrice.

risposta

11

Si potrebbe fare così:

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:

Oppure, si potrebbe anche usare max.col funzione per i vettori in questo modo:

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

edit: Alcuni di 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 

Quindi tutti i metodi sembrano essere più lenti dell'originale (che fornisce risultati errati), ma l'z <- which(x == max(x));z[length(z)] sembra essere l'opzione più veloce di questi.

+0

Il benchmarking è bellissimo! Grazie, penso che andrò con l'opzione 2! 'z <- quale (x == max (x)) z [lunghezza (z)]' – by0

4

La funzione which ha un parametro 'arr.ind' normalmente impostato su FALSE ma utilmente impostato su TRUE in questo caso:

x <- sample(1:20, 50, repl=TRUE) 

> which(x==max(x), arr.ind=TRUE) 
[1] 11 23 
> tail(which(x==max(x), arr.ind=TRUE) , 1) 
[1] 23 

Utilizzare l'argomento arr.ind è particolarmente utile con strutture a matrice o matrice, ma funziona anche con i vettori atomici.

+0

DWin, è c'è una differenza nell'impostazione di 'arr.ind = TRUE' per i vettori? – Arun

+1

Io non la penso così. Probabilmente avrei dovuto usare 'which'. A volte penso troppo. –

4

Si potrebbe invertire x

which.max(rev(x)) 
which.min(rev(x)) 
0

Per espandere sulla risposta di Jouni, si potrebbe invece utilizzare max sul risultato di which:

x <- c(1, 2, 1, 4, 3, 4) 
which(x == max(x)) 
[1] 4 6 
max(which(x == max(x))) 
[1] 6 

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)), max(which(x == max(x))), times = 1000) 
Unit: microseconds 
              expr  min  lq  mean median  uq  max neval 
            which.max(x) 6.322 6.717 7.171838 7.112 7.112 40.297 1000 
{  z <- which(x == max(x))  z[length(z)] } 27.260 28.445 37.126964 28.840 29.630 2276.346 1000 
         tail(which(x == max(x)), 1) 35.952 37.927 45.198484 38.718 40.298 1005.038 1000 
      max.col(matrix(x, nrow = 1), "last") 160.791 162.766 181.698171 163.557 169.087 1688.494 1000 
          max.col(t(x), "last") 84.149 86.124 100.249921 86.915 89.680 1230.618 1000 
           which.max(rev(x)) 53.729 55.310 69.442985 56.100 57.680 1076.149 1000 
          max(which(x == max(x))) 26.865 27.655 35.552256 28.050 28.841 1029.137 1000 
Problemi correlati