Euler_Salter
Euler_Salter

Reputation: 3561

R: given a matrix of 0 and 1s create matrix showing repetition between columns in rows

Setting

The title isn't very informative, so I'm open to editing it. Suppose I have the following data frame

m <- matrix(c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1), nrow=3, ncol=4, 
            dimnames = list(c("row1", "row2", "row3"), c("col1", "col2", "col3", "col4")))
df <- data.frame(m)

It looks like this

     col1 col2 col3 col4
row1    1    0    1    0
row2    0    1    0    0
row3    1    0    1    1

What I want to do

I want to obtain something similar to this (the diagonal can either have all 0s or all 1s, I don't care).

       col1  col2  col3  col4
col1      0     0     2     1
col2      0     0     0     0
col3      2     0     0     1
col4      1     0     1     0

Basically if any two columns have a 1 on the same row (for instance col1 and col3 both have a 1 on row1 and row3) then we add +1 to the corresponding entry in the matrix above. Basically the final matrix counts the number of times that each column has 1s on the same row as other columns.

Upvotes: 2

Views: 40

Answers (1)

akrun
akrun

Reputation: 887501

An option is crossprod after converting to matrix and then change the diagonal elements to 0

`diag<-`(crossprod(as.matrix(df)), 0)
#     col1 col2 col3 col4
#col1    0    0    2    1
#col2    0    0    0    0
#col3    2    0    0    1
#col4    1    0    1    0

Upvotes: 4

Related Questions