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?
I think here comes my answer.
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
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.