I have a list like that:
> print(list)
[[1]]
[1] 1
[[2]]
[1] 4
[[3]]
[1] 1
[[4]]
[1] 2
[[5]]
[1] 2
[[6]]
[1] 3
[[7]]
[1] 2
[[8]]
[1] 5
[[9]]
[1] 1
[[10]]
[1] 2
[[11]]
[1] 3
[[12]]
[1] 7
[[13]]
[1] 3
[[14]]
[1] 4
[[15]]
[1] 3
[[16]]
[1] 5
[[17]]
[1] 1
[[18]]
[1] 1
[[19]]
[1] 4
[[20]]
[1] 6
Now I want to turn this list into an adjacency matrix so that I can then build a graph undirect not simple (there may be multilinks and selfloops).
This list should be read in pairs, that is, (1 4)
indicates that there is a link from node 1 to node 4, the pair (1 2)
indicates that there is a link from node 1 to node 2, etc.
How can I do this?
I thought I iterate the list with a for loop with step = 2 but I haven't found how to do. And I'm not sure how to assign the values of the matrix, what I assing to multilinks?
Thanks a lot
Here's another way
lst <- list(1, 4, 1, 2, 2, 3, 2, 5, 1, 2, 3, 7, 3, 4, 3, 5, 1, 1, 4, 6)
library(igraph)
g <- make_graph(unlist(lst), directed = F)
( m <- as_adjacency_matrix(g, sparse = F) )
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] 1 2 0 1 0 0 0
# [2,] 2 0 1 0 1 0 0
# [3,] 0 1 0 1 1 0 1
# [4,] 1 0 1 0 0 1 0
# [5,] 0 1 1 0 0 0 0
# [6,] 0 0 0 1 0 0 0
# [7,] 0 0 1 0 0 0 0