0

I have a vector of n scores in an arbitrary order and want to calculate the average absolute difference between any two scores. Note, although I may not be using the correct term with "average absolute difference", I am not asking for the mean/median absolute deviation (i.e., mad), which is analogous to a standard deviation. I want deviations from the scores themselves, not from a measure of central tendency.

First, I will want to calculate each absolute difference between any two scores. With a vector of 2 scores, there is only one absolute difference; with 3 scores, there are 3 absolute differences; with 4 scores, there are 6 absolute differences; a general formula is n*(n-1)/2 absolute differences. For example, say I have a vector:

    c(3,4,5,6)

I can calculate each absolute difference manually by taking each score and subtracting it from every other score and then taking the absolute value of the resulting difference. For example:

    |3-4| = 1
    |3-5| = 2
    |3-6| = 3
    |4-5| = 1
    |4-6| = 2
    |5-6| = 1

Second, I will want to take the average of the absolute differences. For example:

    (1 + 2 + 3 + 1 + 2 + 1)/6 = 1.666666666666666

Does anyone know of an R function that can do this? Obviously, when the number of scores n within the vector is small, a manual for loop can be created; but I want to apply the function to vectors with 1000s of scores.

  • 2
    You are likely looking for `dist(x)` (just `mean(dist(x))` solves the second step). – nicola Apr 25 '18 at 05:27
  • Thank you!! That's exactly what I was looking for! For others, I used specifically method = "manhattan" within dist(x) to get what I wanted. – David Disabato Apr 25 '18 at 14:16

0 Answers0