我如何从dist给出的单元格返回行和列号

Too*_*one 3 r

说我有

> x<-1:5
> dist(x)
  1 2 3 4
2 1      
3 2 1    
4 3 2 1  
5 4 3 2 1
> which(dist(x)==max(dist(x)))
[1] 4
Run Code Online (Sandbox Code Playgroud)

如何从索引4返回到行号和列号(5,1)

Mat*_*rde 6

可能有一个更整洁的方式......

dist.x <- dist(x)
which(as.matrix(dist.x) == max(dist.x) & lower.tri(dist.x), arr.ind=TRUE)
#   row col
# 5   5   1
Run Code Online (Sandbox Code Playgroud)