简体   繁体   中英

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 0 s or all 1 s, 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 1 s on the same row as other columns.

An option is crossprod after converting to matrix and then change the diag onal 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM