igraph错误无法创建负数顶点的空图

时间:2014-04-02 17:54:53

标签: r igraph

当我尝试创建下面的简单图表时,为什么会出现错误?如果我用数字替换“a”和“b”那么它有效吗?任何解决方案

g1 <- graph(c("a","b"),directed=TRUE)

错误是

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 个答案:

答案 0 :(得分:4)

?graph,您可以阅读:

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.

由于它正在寻找一个数字向量,但是你已经给它了文本,它将所有字母转换为NA(这是“强制引入的NAs”。

您可以将文本转换为适当的数字标识符,例如:

g1 <- graph(as.numeric(factor(c("a","b"))), directed=TRUE)