Skip to Content

How can I create a distance matrix containing the mean absolute scores between each row?

Given the matrix

     X1 X2 X3 X4 X5
[1,]  1  2  3  2  1
[2,]  2  3  4  4  3
[3,]  3  4  4  6  2
[4,]  4  5  5  5  4
[5,]  2  3  3  3  6
[6,]  5  6  2  8  4

I want to create a distance matrix containing the absolute mean difference between each row of each column. For example, the distance between X1 and X3 should be = 1.67 given that:

abs(1 - 3) + abs(2-4) + abs(3-4) + abs(4-5) + abs(2-3) + abs(5-2) = 10 / 6 = 1.67.

I HAVE TRIED using the designdist function in the vegan package this way:

designdist(t(test), method = "abs(A-B)/6", terms = "minimum")

The resulting distance for columns 1 and 3 is 0.666. The problem with this function is that it sums all the values in each column and then subtracts them. But I need to sum the absolute differences between each row (individually, absolute) and then divide it by N.