Igraph read.graph adds one vertex

When I run this simple example, igraph adds one vertex, and my vertices start at 2 instead of 1

# very very simple graph (1-2-3) edges <- rbind(c(1,2), c(2,3)) write.table(edges, file="edgetest.txt", sep=" ", quote=F, row.names=F, col.names = F) g <- simplify(read.graph(file="edgetest.txt", format="edgelist", directed=F)) plot(g) 

Here's what it looks like after running the example

enter image description here

Does anyone know why this is happening? Is this normal or am I missing something

+4
source share
2 answers

I think you are introducing some error while writing text and reading. You could just do:

 edges <- rbind(c(1,2), c(2,3)) g <- graph.edgelist(edges) plot(g) 
+1
source

read.edgelist() expects a text file where vertex identifiers start from zero. If you want to write a list of edges from the matrix to a file, subtract 1:

 write.table(edges-1, file="edgetest.txt", sep=" ", quote=F, row.names=F, col.names = F) 
+4
source

Source: https://habr.com/ru/post/1447077/


All Articles