0

I have a dataframe with multiple dummy variables.

df <- data.frame(A=c(1,0,1,0,1),B=c(1,1,1,0,0),C=c(0,1,0,1,1))

Acutually, the number "1" in each row represent the occurrence of varables, thus the dataframe is something like co-occurance dataframe.

I hope to transfer it into adjacency matrix.

,A,B,C

A,3,2,1

B,2,3,1

C,1,1,3

How can I do it?

update

I think here comes my answer.

Zhiliang Lin
  • 309
  • 2
  • 10

1 Answers1

0

I think I am missing some easy way to do this but one option with base R outer can be

cols <- names(df)
apply_fun <- function(x, y) sum(df[x] == 1 & df[y] == 1)
mat <- outer(cols,cols, Vectorize(apply_fun))
row.names(mat) <- cols
colnames(mat) <- cols

mat
#  A B C
#A 3 2 1
#B 2 3 1
#C 1 1 3
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213