我一直在努力使用 igraph,似乎没有找到可以为我提供所需结果的文档。
我有一个 igraph 对象,比方说 g1,它包含从邻接矩阵获得的网络。我知道我需要应用 E 函数来获取所有边,例如,当我应用 E(g1)[1] 时,我得到(示例中的变量已匿名):
我想获取顶点索引,并且我发现如果我查看上面列表中的第一个元素,我会得到
E(g1)[1][[1]]
**我想访问“tid”和“hid”值。 **我知道这可能是一个愚蠢的问题,但我尝试了很多命令,但似乎无法理解对象是什么类型
E(g1)[1][[1]]。
这样做的原因是,为了显示数据集中的一些预期关系,我想解析我的边缘列表并将 hid = tid+108 的颜色更改为红色,并且使用这些对象似乎是最快的/最简单的方法。
一种方法是将图的边列表转换为数据框,并使用 R 基础计算来查找所需的边。
library(igraph)
g <- sample_gnp(10, 6/10) # Graph to test.
df1 <- as_edgelist(g) # Edgelist to dataframe.
# Edge (eid) / Vertex ids. are sequentially numbered starting with 1.
# Diff is the difference between tail and head id.
df2 <- cbind(eid = seq_len(gsize(g)), hid=df1[,1], tid=df1[,2], diff = df1[, 2] - df1[, 1] )
idx <- df2[, "diff"] == 3 # Calculate row ids based on a given row condition.
red <- df2[idx, "eid"] # Compute the row of ids that satisfies the given condition.
E(g)[red]
# + 4/30 edges from 4099ca9:
# [1] 3-- 6 5-- 8 6-- 9 7--10
E(g)$color <- "black" # Default color.
E(g)[red]$color <- "red" # Set color.
plot(g) # Show plot.