0
votes

I have adjacency list in the form of:

1.  3,4
2.  4
3.  1,4
4.  1,2,3

and I want to transform into adjacency matrix using R. I have tried various commands like transformation of adjacency list to igraph object and then retransformation of igraph to adjacency matrix, but the obtained adjacency matrix is S4 class. I want simple commands to transform adjacency list to adjacency matrix in R.

data

 list(c(1L, 3L, 4L, 8L, 14L, 31L, 2L, 29L, 33L, 7L, 11L, 17L, 
5L, 6L, 34L), c(2L, 3L, 4L, 8L, 9L, 12L, 13L, 14L, 18L, 22L, 
1L, 10L, 33L, 34L), c(2L, 3L, 4L, 8L, 9L, 12L, 13L, 14L, 18L, 
20L, 22L, 32L, 1L, 31L, 34L, 24L), c(2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 1L, 31L, 10L, 28L, 
29L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 
22L, 32L, 1L, 17L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 
14L, 18L, 20L, 22L, 32L, 1L, 17L), c(4L, 5L, 6L, 7L, 8L, 9L, 
11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L, 17L), c(2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L, 
31L, 10L, 28L, 29L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 
13L, 14L, 18L, 20L, 22L, 32L, 10L, 28L, 29L, 33L, 34L, 15L, 16L, 
19L, 21L, 23L, 24L, 30L, 31L, 27L), c(2L, 4L, 8L, 9L, 10L, 14L, 
28L, 29L, 33L, 15L, 16L, 19L, 20L, 21L, 23L, 24L, 27L, 30L, 31L, 
32L), c(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 
22L, 32L, 1L, 17L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 
13L, 14L, 18L, 20L, 22L, 32L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L), c(2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 1L, 31L, 
10L, 28L, 29L, 33L, 15L, 16L, 19L, 21L, 23L, 24L, 27L, 30L), 
    c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 31L, 32L, 10L, 14L, 
    20L, 27L, 28L, 29L), c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 
    30L, 31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L), c(1L, 7L, 11L, 
    17L, 5L, 6L), c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 
    13L, 14L, 18L, 20L, 22L, 32L, 31L), c(9L, 15L, 16L, 19L, 
    21L, 23L, 24L, 30L, 31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L
    ), c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 
    20L, 22L, 32L, 31L, 10L, 15L, 16L, 19L, 21L, 23L, 24L, 27L, 
    28L, 29L, 30L), c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 
    31L, 32L, 10L, 14L, 20L, 27L, 28L, 29L), c(2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 20L, 22L, 32L, 31L
    ), c(9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 31L, 32L, 10L, 
    14L, 20L, 27L, 28L, 29L), c(24L, 25L, 32L, 3L, 34L, 27L, 
    33L, 9L, 15L, 16L, 19L, 21L, 23L, 30L, 31L, 10L, 14L, 20L, 
    28L, 29L), c(24L, 25L, 32L, 34L, 26L, 29L), c(26L, 28L, 30L, 
    33L, 34L, 32L, 25L, 29L), c(24L, 27L, 33L, 9L, 10L, 14L, 
    15L, 16L, 19L, 20L, 21L, 23L, 28L, 29L, 30L, 31L, 32L), c(4L, 
    8L, 9L, 10L, 14L, 28L, 29L, 33L, 26L, 30L, 32L, 15L, 16L, 
    19L, 20L, 21L, 23L, 24L, 27L, 31L), c(1L, 4L, 8L, 9L, 10L, 
    14L, 28L, 29L, 33L, 25L, 26L, 15L, 16L, 19L, 20L, 21L, 23L, 
    24L, 27L, 30L, 31L, 32L), c(26L, 28L, 30L, 33L, 34L, 9L, 
    15L, 16L, 19L, 21L, 23L, 24L, 31L, 32L, 10L, 14L, 20L, 27L, 
    29L), c(1L, 3L, 4L, 8L, 14L, 18L, 20L, 22L, 31L, 33L, 34L, 
    9L, 15L, 16L, 19L, 21L, 23L, 24L, 30L, 32L, 10L, 27L, 28L, 
    29L), c(3L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 
    20L, 22L, 32L, 26L, 28L, 24L, 25L, 15L, 16L, 19L, 21L, 23L, 
    30L, 31L, 10L, 27L, 29L), c(1L, 2L, 9L, 10L, 14L, 28L, 29L, 
    33L, 31L, 34L, 26L, 30L, 24L, 27L), c(1L, 3L, 31L, 33L, 34L, 
    2L, 26L, 30L, 24L, 25L, 9L))
1
Please use dput to show the exampleakrun
Can you check tha t hte data I added to your question is correct please as the code in your comment throws error. [as_adjacency_matrix(graph_from_adj_list(lst)) ]user20650
as_adjacency_matrix(graph_from_adj_list(lst)) This is not giving error. Instead it gives S4 class objectkumar
@kumar ; it returns the adjacency in sparse matrix form , which is a good default. You can transform this in to a standard matrix, by wrapping the output in as.matrix, or sing the sparse argument ; ie as_adjacency_matrix(graph_from_adj_list(lst), sparse = FALSE)user20650

1 Answers

0
votes

Suppose el is a list of edge list:

el = list(c(3,4),
          c(2,4),
          c(1,4),
          c(1,2,3))

#Get the matrix dimension
dim <- length(el)

m <- sapply(el, function(x) { r<-rep(0,dim); r[unlist(x)]<-1;r})
     [,1] [,2] [,3] [,4]
[1,]    0    0    1    1
[2,]    0    1    0    1
[3,]    1    0    0    1
[4,]    1    1    1    0