说我有
> 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)?
可能有一个更整洁的方式......
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)