防止 R igraph 添加零顶点
我正在使用以下代码创建一个图表:
g <- graph(c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5 ,2))
我想知道如何创建此图,而不必执行以下额外步骤:
delete.vertices(g, 0)
该图函数似乎是默认的到零索引顶点,因此图中存在不与任何其他顶点连接的顶点 0。我可以使用 V(g) 或简单的summary(g) 来验证这一点,它显示 id 从 0 到 5 的 6 个顶点。
I am creating a graph using the follow code:
g <- graph(c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2))
I would like to know how I can create this graph and not have to go the extra step of:
delete.vertices(g, 0)
The graph function seems to default to zero indexed vertices and so a vertex 0 exists in the graph that is not connected with any other vertices. I can verify this using V(g), or simply summary(g), which show 6 vertices with ids from 0 to 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从 r-forge 站点访问的文档说:“顶点和边在 igraph 中具有数字顶点 id。顶点 id 始终是连续的,并且从零开始。”因此,在将向量发送到图形函数之前,您需要从该向量中减去 1。否则,您最终会得到一个未连接的“0”顶点。避免未连接的“0”顶点的一般方法是从向量中减去最小值。
所以我怀疑你的问题可能真的是“如何使用我自己的规则来标记顶点?”。试试这个:
文档的开头部分还说:
The documentation that I access from the r-forge site says: "Vertices and edges have numerical vertex ids in igraph. Vertex ids are always consecutive and they start with zero." So you need to subtract 1 from that vector before sending it to the graph function. Otherwise you end up with an unconnected "0" vertex. The general method for avoiding an unconnected "0" vertex would be to subtract the minimum from the vector.
So I suspect you question might really be "How can I label vertices using my own rules?". Try this:
The opening section of the docs also says: