20

which.maxwhich.min如果有关系,将返回最大值或最小值的最小索引。

有没有办法解决这个问题,以便在影响函数效率的情况下返回最大的索引?

max.col有这个确切的功能,但我正在处理一个向量而不是一个矩阵。

4

4 回答 4

17

你可以这样做:

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

编辑:

或者,您也可以max.col对这样的向量使用函数:

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

编辑:一些基准测试:

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

因此,所有方法似乎都比原始方法慢(给出错误的结果),但z <- which(x == max(x));z[length(z)]似乎是这些方法中最快的选择。

于 2013-03-26T05:40:29.320 回答
9

你可以逆转x

which.max(rev(x))
which.min(rev(x))
于 2013-03-26T05:45:22.810 回答
5

which函数有一个“arr.ind”参数,通常设置为 FALSE,但在这种情况下设置为 TRUE 很有用:

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

使用 arr.ind 参数对矩阵或数组结构特别有用,但它也适用于原子向量。

于 2013-03-26T05:39:27.553 回答
3

要扩展 Jouni 的答案,您可以改为使用max以下结果which

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

基准测试:

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
于 2017-05-04T09:18:37.730 回答