1
votes

I have a dataset of a graph (undirected), represented by the edges. But most of the vertices are not connected.

Supposing the vertex set is

{1,2,3,4,5,6,7,8}

and the edges are just

1->2    
2->3    
1->3

How do I get the adjacency matrix for this using the igraph package? Using the below code doesnt give the full adjacency as it gets only 3 vertices in the input edgelist

get.adjacency(graph.edgelist(as.matrix(edges), directed=FALSE))
1

1 Answers

2
votes

The ?graph.edgelist shows other constructors for graphs. It seems that graph has what you need:

library(igraph)
edges <- data.frame(v1 = c(1, 2, 1), v2 = c(2, 3, 3))

g <- graph(edges = t(as.matrix(edges)), n = 8, directed = FALSE)
get.adjacency(g)

# 8 x 8 sparse Matrix of class "dgCMatrix"
#                     
# [1,] . 1 1 . . . . .
# [2,] 1 . 1 . . . . .
# [3,] 1 1 . . . . . .
# [4,] . . . . . . . .
# [5,] . . . . . . . .
# [6,] . . . . . . . .
# [7,] . . . . . . . .
# [8,] . . . . . . . .