Given the matrix,
df <- read.table(text="
X1 X2 X3 X4 X5
1 2 3 2 1
2 3 4 4 3
3 4 4 6 2
4 5 5 5 4
2 3 3 3 6
5 6 2 8 4", header=T)
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(df), 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.
Here's a one-line solution. It takes advantage of
dist()
'smethod
argument to calculate the L1 norm aka city block distance aka Manhattan distance between each pair of columns in your data.frame.To make it reproducible: