0

I haven't learned how to work with R yet. I intend to in the future, but this specific problem requires my more immediate attention.

I'm trying to take two columns of data to be imported into Gephi for visualization. Specifically, I'm trying to convert this:

A x
A y
A z

B x
B y
B w

into this:

  x y z w
x 0 2 1 1
y 2 0 1 1
z 1 1 0 0
w 1 1 0 0

Afterwards, I need to take the top triangle of the matrix and put it in this format:

x y 2
x z 1
x w 1
y z 1
y w 1
z w 0

I don't know how any of these processes are called, so my searches here and on Google came up empty.

Can someone please point me in the right direction please?

1 Answers1

1

Found it. This is called a co-occurrence matrix. The diagonal elements are zero because I'm counting how many times they appear together with others in a group, but not themselves. The code below worked:

V <- crossprod(table(dat[1:2]))
diag(V) <- 0
V

Source

I ran the code below to end up with a lower triangle matrix

V[upper.tri(V)] = NA

And then this to end up with the co-correspondence columns

library(reshape2)
df <- melt(V, na.rm=TRUE)

Source

Community
  • 1
  • 1