This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Creating adjacency matrix from collapsed row values in R

Hi all,

I have a data frame with two columns representing one to many relationship values. Here is a snippet to generate the data as I have it:

get_genes <- function(i) paste0(sample(LETTERS[6:15], sample(3:7, 1), replace=TRUE)
                              , collapse = ";")
dummy_data <- data.frame(X1 = sample(LETTERS[1:5])
                       , X2 = sapply(1:5, get_genes)
                       )

Which would produce a dataframe similar to this:

  X1   X2
1  E   L;M;G
2  B   J;K;O
3  C   K;O;F
4  D   F;N;G;M
5  A   N;M

I need to transform that dataframe into an adjacency matrix like the example below. I have no idea how to go about it, though. I have played around with reshape2, but I can't find the right way to get what I need. Any solution will be greatly appreciated.

   X1 X2
1   E  L
2   E  M
3   E  G
4   B  J
5   B  K
6   B  O
7   C  K
8   C  O
9   C  F
10  D  F
11  D  N
12  D  G
13  D  M
14  A  N
15  A  M
adjacency reshape2 matrix r

1 answer

If anyone else comes across this issue, I found a way to solve it by creating a list from each of the vectors in X2 and using the solution proposed here. Code:

df <- data.frame(tibble(x1=dummy_data$X1, x2=strsplit(dummy_data$X2, ";")))
library("tidyverse")
t <- data.frame(unnest(df, c( "x2")))

Log in to answer this question.