您可以使用combn 创建行对并计算每对行的堪培拉距离。然后转换为dist 类,使用稀疏Matrix 包将索引和值转换为矩阵
#OP's data
set.seed(1)
canb.dist <- function(x, j) sum((abs(x-j))/(abs(x)+abs(j)))
data <- data.frame(replicate(500,sample(1:100,50,rep=TRUE)))
refdist <- dist(data, method="canberra")
#convert to matrix
mat <- as.matrix(data)
#sequence of row indices
rowidx <- seq_len(nrow(mat))
#calculate OP's Canberra dist for each pair of rows
triangular <- combn(rowidx, 2, function(x) c(x[1], x[2], canb.dist(mat[x[1],], mat[x[2],])))
#construct the matrix given the indices and values using Matrix library,
#convert into a matrix before converting into a dist class
#the values refer to the diagonal, lower triangular and upper triangular
library(Matrix)
ansdist <- as.dist(as.matrix(sparseMatrix(
i=c(rowidx, triangular[1,], triangular[2,]),
j=c(rowidx, triangular[2,], triangular[1,]),
x=c(rep(0, length(rowidx)), triangular[3,], triangular[3,])
)))
#idea from http://stackoverflow.com/questions/17375056/r-sparse-matrix-conversion/17375747#17375747
range(as.matrix(refdist) - as.matrix(ansdist))