我想得到一个向量的某个子集的最小值的索引,但是原始向量中的索引,而不是重新编号的子集.
至于现在,我一直在使用:
L = rnorm(20) # say this is the original vector
subset = runif(20)<0.3 # some conditions to extract the subset
ind_min = which.min(L[subset])
ind_sel = seq(L)[subset]
ind_min = ind_sel[ind_min]
Run Code Online (Sandbox Code Playgroud)
但我想应该有更直接或更清洁的东西.我一直在考虑使用如下的技巧:
L_tmp = L
L_tmp[!subset] = Inf
ind_min = which.min(L_tmp)
Run Code Online (Sandbox Code Playgroud)
这显然更有效:
> microbenchmark(method_1(), method_2(), unit = "relative")
Unit: relative
expr min lq mean median uq max neval
method_1() 3.699562 3.249635 3.119666 3.076819 2.928259 3.225849 100
method_2() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 100
Run Code Online (Sandbox Code Playgroud)
但我对此并不满意,因为我猜应该还有别的东西.有什么建议吗?
你可以试试:
(seq(L))[subset][which.min(L[subset])]
Run Code Online (Sandbox Code Playgroud)
这与您的第一个方法类似,但没有创建临时变量
20000长向量的基准结果L:
method_cath<- function(){(seq(L))[subset][which.min(L[subset])]}
method_FK_corr1 <- function(){min = min(L[subset])
ind_min = intersect(which(L == min), seq(L)[subset])[1]
return(ind_min)}
method_FK_corr2 <- function(){min = min(L[subset])
ind_min = intersect(which(L == min), which(subset))[1]
return(ind_min)}
method_1clm <- function(){ind_min = which.min(L[subset])
ind_sel = seq(L)[subset]
ind_min = ind_sel[ind_min]
return(ind_min)}
method_2clm <- function(){L_tmp = L
L_tmp[!subset] = Inf
ind_min = which.min(L_tmp)
return(ind_min)}
> microbenchmark(method_2clm(), method_cath(), method_1clm(), method_FK_corr2(), method_FK_corr1(), unit = "relative")
# Unit: relative
# expr min lq mean median uq max neval cld
# method_2clm() 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 100 a
# method_cath() 1.312146 1.290370 1.282964 1.278178 1.282424 0.9191693 100 b
# method_1clm() 1.295031 1.294642 1.303781 1.284630 1.279821 1.2977193 100 b
# method_FK_corr2() 1.185821 1.166924 1.278030 1.155217 1.165738 4.9948007 100 b
# method_FK_corr1() 1.683783 1.644797 1.746055 1.635293 1.636195 5.1616672 100 c
Run Code Online (Sandbox Code Playgroud)
注意:我得到NA了@FedorenkoKristina原始函数的结果,我测试了2个可能的校正函数,现在所有函数都给出了相同的结果.