如果我们需要matrix,我们可以使用outer
outer(seq_along(df1), seq_along(df1), FUN =
Vectorize(function(i, j) sum(df1[[i]] - df1[[j]], na.rm = TRUE)))
-输出
[,1] [,2] [,3]
[1,] 0.00 47.80 56.49
[2,] -47.80 0.00 8.69
[3,] -56.49 -8.69 0.00
或者如果我们不需要冗余比较,使用combn
combn(df1, 2, FUN = function(x) sum(x[[1]] - x[[2]], na.rm = TRUE))
-输出
[1] 47.80 56.49 8.69
数据
df1 <- structure(list(V1 = c(67.81, 65.33, 54.67, 53.2, 53.77, 52.66,
50.77, 47.84, 46.33, 44.15), V2 = c(57.68, 56.58, 52.61, 49.74,
49.28, 48.03, 46.15, 43.96, 42.76, 41.94), V3 = c(54.04, 54.34,
52.36, 49.34, 48.93, 48.06, 46.21, 43.51, 42.15, 41.1)),
class = "data.frame", row.names = c(NA,
-10L))