Difference Matrix between all elements of a column

Viewed 51

I have a data frame df

ID  scores
1   2.1
2   1.3
3   -1
4   -3
5   2.4

I am interested in calculating a difference matrix that contains the difference of each element from the column scores with every element of the same column (including itself).

My desired output is something like this:

    1     2    3    4    5
1   0     0.8  3.1  5.1 -0.3
2  -0.8   0    2.3  4.3 -1.1
3  -3.1  -2.3  0    2   -3.4
4  -5.1  -4.3 -2    0   -5.4
5   0.3   1.1  3.4  5.4  0

The following post is relevant but asks to compute the differences in another way Find the differences in all possible ways of list elements

Is there an easy way to achieve this output, perhaps by using dplyr or some built-in function? Any help or guidance is greatly appreciated!

1 Answers

You could use outer, which is a base R function for passing all pairwise combinations of the elements of two vectors to a binary function such as -:

df <- data.frame(ID = 1:5, scores = c(2.1, 1.3, -1, -3, 2.4))

outer(df$scores, df$scores, `-`)
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.0  0.8  3.1  5.1 -0.3
[2,] -0.8  0.0  2.3  4.3 -1.1
[3,] -3.1 -2.3  0.0  2.0 -3.4
[4,] -5.1 -4.3 -2.0  0.0 -5.4
[5,]  0.3  1.1  3.4  5.4  0.0
Related