Vitomir Kovanovic
Vitomir Kovanovic

Reputation: 421

igraph read.graph adds one vertex

When I run this simple example, igraph adds one vertex and my vertices start from 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)

This is how it looks like after running the example

enter image description here

Does someone knows why this happens? Is this OK or am I missing something

Upvotes: 4

Views: 429

Answers (2)

Gabor Csardi
Gabor Csardi

Reputation: 10825

read.edgelist() expects a text file, where the vertex ids start at zero. If you want to write an edge list from a matrix to a file, subtract 1:

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

Upvotes: 4

thelatemail
thelatemail

Reputation: 93813

I think you are introducing some error by writing out to text and reading back in. You could just do:

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

Upvotes: 1

Related Questions