r - iGraph 创建子图

标签 r igraph

我有以下问题:
我有以下图表:g6其中不包括isolate vertices 。顶点主要分为两个ethnic groups ,作为属性给出,即:mestizo and saraguro .
所以我运行 subgraph saraguros 的选项如下:

g2saraguro <- subgraph(g6, which(V(g6)$ethnic=="saraguro"))
summary(g2saraguro)

确实有效:

Vertices: 166
Edges: 330
Directed: TRUE
No graph attributes.
Vertex attributes: ii, village, parroquia, canton, provinz, kindR, ethnic, situation, researcha.
Edge attributes: question, activity.

但是,如果我尝试使用 mestizo 进行同样的操作,有一个问题,因为它只是识别:

s2mestizo <- subgraph(g6,V(g6)$ethnic=="mestizo")
summary(s2mestizo)
Vertices: 2
Edges: 3
Directed: TRUE
No graph attributes.
Vertex attributes: ii, village, parroquia, canton, provinz, kindR, ethnic, situation, researcha.
Edge attributes: question, activity.

但实际上情况并非如此,如果我看到:V(g6)$ethnic , mestizo是大多数!!
我真不知道问题出在哪里,用igraph 0.6形成 subgraph 的程序应该以这种方式或类似的方式,没有 1-based indexing .
非常感谢您的帮助
问候
弗拉基米尔

最佳答案

在第二种情况下你忘记使用which函数;即,而不是

s2mestizo <- subgraph(g6,V(g6)$ethnic=="mestizo")

你应该写

s2mestizo <- subgraph(g6, which(V(g6)$ethnic=="mestizo"))

另请注意,如果您使用 igraph 0.5.x 或更早版本,则必须从 which 的结果中减去 1,因为 igraph 的索引基于 0。此问题已在 igraph 0.6 中修复,因此如果您使用 igraph 0.6 或更高版本,则无需减去 1。

编辑:根据您最近的评论,您似乎使用的是 igraph 0.5.x 或更早版本(因为 summary 的输出格式与 0.6 不同)。在这种情况下,您必须which的结果中减去1,因为igraph 0.5.x及更早版本使用从零开始的顶点索引。因此,正确的行应该是:

s2mestizo <- subgraph(g6, which(V(g6)$ethnic=="mestizo")-1)

关于r - iGraph 创建子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12368680/

相关文章:

r - OpenCPU:没有方法 asJSON S3 类

r - 在 data.table 中使用 by 连接继承范围

r - 如何将函数应用于图形/网络中的每个连接组件?

r - 通过原点节点指定圆弧颜色 arcplot {arcdiagram}

python - igraph(最大)生成树断开连接

r - 如何在R中按两列分组

r - 使用 lubridate 解析 12 小时时间

c - 如何从 C 和 Igraph 中的两个 id 顶点获取边的 id?

r - pca - 哪个特征向量对应于哪个特征值

r - 在 R 中绘制 igraph 对象时控制轴标签大小