Skip to content
Advertisement

Create a matrix of pairwise comparisons between columns

I would like to create a matrix showing the number of row-wise differences for each pairwise comparison of columns. This is what I’m starting with:

     Ind1 Ind2 Ind3
Att1    A    A    B
Att2    A    C    C
Att3    B    B    D

This is what I want to end up with:

      Ind1  Ind2  Ind3
Ind1            
Ind2    1       
Ind3    3     2 

How can I do this in Python or R?

Advertisement

Answer

Try adist like below

> adist(sapply(df, toString))
     Ind1 Ind2 Ind3
Ind1    0    1    3
Ind2    1    0    2
Ind3    3    2    0
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement