Reputation: 3561
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
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.
Upvotes: 2
Views: 40
Reputation: 887501
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
Upvotes: 4