标题清楚地说明了。我的问题很简单。假设我有矢量x:
x <- c(13,10,9,5)
# this function gives only distances
dist(x)
1 2 3
2 3
3 4 1
4 8 5 4
Instead of distance I would like to have sums like this:
# sum matrix
1 2 3
2 23
3 22 19
4 18 15 14
怎么做? dist
函数中是否有一些参数?
答案 0 :(得分:1)
使用外部功能
as.dist(outer(x,x,'+'))
答案 1 :(得分:1)
您可以使用outer
sums <- outer(x, x, `+`)[-1, ]
要使其对称,请使用uppertri
sums[upper.tri(sums)] <- NA