Edward
Edward

Reputation: 4623

Graph / matrix from dataframe by conditiom

I have a dataframe like this

df = data.frame(id = c(1,1,1,2,2,2,3,3,3), y=c('a','b', 'b', 'a', 'b', 'c', 
                'a', 'b', 'd'))

I want to get a matrix / graph as below

  a b c d
a 0 4 1 1
b 4 0 1 1
c 1 1 0 0
d 1 1 0 0

where 'a', 'b', 'c', 'd' are levels of df$y and cell is filling by df$id. For example the cell 'a'-'b' = 4 because for id 1 'a' and 'b' intersect twice, for id 2 'a' and 'b' intersect one time, for id 4 'a' and 'b' intersect one time, total 4 times. Etc.... Thanx for any idea...

Upvotes: 2

Views: 34

Answers (1)

akrun
akrun

Reputation: 887118

An option is to get the crossprod on table and then multiply with a diag

out <- crossprod(table(df)) * !diag(length(unique(df$y)))
out
# y
#y   a b c d
#  a 0 4 1 1
#  b 4 0 1 1
#  c 1 1 0 0
#  d 1 1 0 0

and remove the 'y' attributes by setting it to NULL

names(dimnames(out)) <- NULL

Upvotes: 3

Related Questions