Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference Matrix between all elements of a column

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading