R 获取已达到最大值的两个索引

R 获取已达到最大值的两个索引,r,indexing,R,Indexing,我在输出中获取两个索引时遇到了问题,我们有最大值。让我们考虑下面的代码。< /P> choose_coef<-function(vec){ t<-seq(from=1,to=2,by=0.5) for (i in 1:length(t)){ for (j in 1:length(t)){ vec<-append(vec,i/2-j/2) } } #which i,j gives max(vec) } choose_coef不清楚为什么要向

我在输出中获取两个索引时遇到了问题,我们有最大值。让我们考虑下面的代码。< /P>
choose_coef<-function(vec){
  t<-seq(from=1,to=2,by=0.5)
  for (i in 1:length(t)){
    for (j in 1:length(t)){
    vec<-append(vec,i/2-j/2)
    }
  }
#which i,j gives max(vec) 
}

choose_coef不清楚为什么要向这个函数传递一个向量,然后在它的末尾追加内容。我猜您希望
vec
为空

一个简单的解决方案是这样的修改:

选择_coef
choose_coef()
#> The maximum value was 1 which occurred at position 7 
#> Since there were 3 iterations of j for each of the 3 iterations of i,
#> we know that the maximum occurred on:
#> i = 3 and j = 1