2

I have the following matrix:

0 1 0 0 0 1 0 0   # Row A
0 1 0 0 0 0 1 0   # Row B
0 1 0 0 0 0 0 0   # Row C
0 0 1 0 0 0 0 0   # Row D

I want to make a new matrix that shows the pairwise difference between each row (e.g between rows A and B, there are 2 columns that are different, so the entry in the matrix corresponding to A and B is 2). Like this:

   A B C D
A  - 2 1 3
B  - - 1 3
C  - - - 2
D  - - - -

The matrix isn't absolutely necessary. It's just an intermediary step for what I really want to do: count the number of pairwise differences between each row in the original matrix like so...

(2+1+3+1+3+2) = 12

cooldood3490
  • 2,418
  • 7
  • 51
  • 66

2 Answers2

3

You could try combn

 v1 <- combn(1:nrow(m1), 2, FUN=function(x) sum(m1[x[1],]!= m1[x[2],]))
 v1 
 #[1] 2 1 3 1 3 2
 sum(v1)
 #[1] 12

If you need a matrix output

m2 <- outer(1:nrow(m1), 1:nrow(m1), FUN=Vectorize(function(x,y)
           sum(m1[x,]!=m1[y,])))

dimnames(m2) <- rep(list(LETTERS[1:4]),2)
m2[lower.tri(m2)] <- 0
m2
#  A B C D
#A 0 2 1 3
#B 0 0 1 3
#C 0 0 0 2
#D 0 0 0 0

data

 m1 <- structure(c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
 0L, 0L, 0L), .Dim = c(4L, 8L))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

I think that this function could help you to count the differences

count.diff <- function(mat) {
  Nrow <- nrow(mat)
  count <- 0
  for (i in 1:(Nrow-1)) count <- count + sum(t(t(mat[-(1:i),])!=mat[i,]))
  count
}

mat <- matrix(rbinom(n=24,size=1,prob=0.7), ncol=4)
mat
count.diff(mat)
Freddy
  • 401
  • 4
  • 12