igraph error cannot create empty graph with negative number of vertices

人走茶凉 提交于 2019-12-10 22:29:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!