user2380782
user2380782

Reputation: 1446

make asymmetric pairwise comparisons in a matrix

I have a matrix with pairwise values but no symmetric, I mean, AxB is not the same as BxA. Here is a dummy example:

    A    B    C    D    E
A    1  0.7  0.8  0.8  0.9
B  0.2    1  0.2  0.8  0.3
C  0.3  0.4    1  0.5  0.6
D  0.4  0.9  0.8    1  0.4
E  0.8  0.2  0.8  0.8    1

I need to know how many [i,j] comparisons are higher or equal than 0.7 and how many comparisons [j,i] are also higher than 0.7 in order to remove high redundant elements. In the example, element A has a higher number of elements with B, C, D, and E and it should be removed in further analysis. I've thought in using table command for this

table1 <- apply(M, 1, table)
table2 <- apply(M, 2, table)

And then compare table1 with table2 for removing the redundant elements, does it would be a good approach?

Thanks

Upvotes: 1

Views: 344

Answers (1)

Chase
Chase

Reputation: 69171

What do you want to do with the entries > 0.7? Set them to NA? Remove them? Regardless, this should get you going in the right direction:

#Assumes your data is named x
> which(x > 0.7, arr.ind = TRUE)
#----
  row col
A   1   1
E   5   1
B   2   2
D   4   2
...

#Set values = NA
x[which(x>0.7, arr.ind = TRUE)] <- NA
#---
    A   B   C   D   E
A  NA 0.7  NA  NA  NA
B 0.2  NA 0.2  NA 0.3
C 0.3 0.4  NA 0.5 0.6
D 0.4  NA  NA  NA 0.4
E  NA 0.2  NA  NA  NA

Upvotes: 1

Related Questions