2

I use the asnipe package to create networks from group-by-individual matrix, but then, I need to convert it as an adjacency matrix. To do so, I thought about using the functions included in igraph such as get.adjacency, but it returns a non-weighted matrix, where all the associations only take 1 as a value.

For instance, if we consider the following dataset:

data <- read.table(text = "  A B C D E
                 1 1 1 0 0 0
                 2 1 1 0 0 0
                 3 1 1 0 0 0
                 4 1 1 0 0 0
                 5 0 0 1 1 1
                 6 0 0 1 1 1
                 7 0 0 1 1 1
                 8 0 0 1 1 1
                 9 1 1 1 1 1", header = TRUE)

Which gives the following graph:

network <- get_network(association_data = data, data_format = "GBI")
# Generating  5  x  5  matrix

library(igraph)
net <- graph.adjacency(network, mode = "undirected", weighted = TRUE, diag = FALSE)

graph

I would expect to get a matrix like this one:

  A B C D E
A . 5 1 1 1
B 5 . 1 1 1
C 1 1 . 5 5
D 1 1 5 . 5
E 1 1 5 5 .

But I get this:

get.adjacency(net)
# 5 x 5 sparse Matrix of class "dgCMatrix"
  A B C D E
A . 1 1 1 1
B 1 . 1 1 1
C 1 1 . 1 1
D 1 1 1 . 1
E 1 1 1 1 .

This data set is an example, the real data contains thousands of lines, excluding any possibility to do it manually. Thank you a lot by advance!

Henrik
  • 65,555
  • 14
  • 143
  • 159
Dolven
  • 39
  • 3
  • 2
    I'm sure someone can help you get a sparse matrix. Meanwhile, straight from your 'data': `m <- crossprod(as.matrix(data))`; `diag(m) <- 0`. [Create a co-occurrence matrix from dummy-coded observations](https://stackoverflow.com/questions/10622730/create-a-co-occurrence-matrix-from-dummy-coded-observations) – Henrik Nov 27 '18 at 07:36
  • @Henrik `library(Matrix); drop0(Matrix(m))` – Roland Nov 27 '18 at 08:00
  • Thanks a lot for this function! – Dolven Nov 27 '18 at 08:57

0 Answers0