Convert a DataFrame into Adjacency/Weights Matrix in R
7
I have a DataFrame, df . n is a column denoting the number of groups in the x column. x is a column containing the comma-separated groups. df <- data.frame(n = c(2, 3, 2, 2), x = c("a, b", "a, c, d", "c, d", "d, b")) > df n x 2 a, b 3 a, c, d 2 c, d 2 d, b I would like to convert this DataFrame into a weights matrix where the row and column names are the unique values of the groups in df$c , and the elements represent the number of times each of the groups appear together in df$c . The output should look like this: m <- matrix(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 2, 1, 1, 2, 0), nrow = 4, ncol = 4) rownames(m) <- letters[1:4]; colnames(m) <- letters[1:4] > m a b c d a 0 1 1 1 b 1 0 0 1 c ...