说我有
> 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
如何从索引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
关于r - 如何从 dist 给出的单元格返回行号和列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191560/