问题
Why do I get an error when I try to create below simple graph? If i replace "a" and "b" with numbers then it works? any solution
g1 <- graph(c("a","b"),directed=TRUE)
error is
Error in graph(c("a", "b"), directed = TRUE) :
At type_indexededgelist.c:117 : cannot create empty graph with negative number of vertices, Invalid value
In addition: Warning messages:
1: In graph(c("a", "b"), directed = TRUE) : NAs introduced by coercion
2: In graph(c("a", "b"), directed = TRUE) : NAs introduced by coercion
回答1:
From ?graph, you can read:
edges: Numeric vector defining the edges, the first edge points from the first element to the second, the second edge from the third to the fourth, etc.
Since it's looking for a numeric vector but you've given it text, it's converting all the letters to NA (this is the "NAs introduced by coercion."
You could convert the text to appropriate numeric identifiers with something like:
g1 <- graph(as.numeric(factor(c("a","b"))), directed=TRUE)
来源:https://stackoverflow.com/questions/22819258/igraph-error-cannot-create-empty-graph-with-negative-number-of-vertices