从R中的igraph对象提取带有条件的边列表

时间:2019-02-28 16:05:54

标签: r attributes igraph edge-detection

我正在使用由不同类型的节点组成的无向igraph对象(例如,雄性M为黄色,雌性F为橙色):

g <- graph.atlas(711)
V(g)$name <- 1:7
V(g)$gender <- c("M","F","M","M","M","F","F")
V(g)$color <- ifelse(V(g)$gender=="F", "orange","yellow")
g<-delete.edges(g, E(g, P=c(1,2,2,3,2,7,7,6,7,3,3,4,3,5,4,5,5,6,6,1))) 
g<-add.edges(g,c(1,4,4,5,5,1,4,7,7,3,3,5,5,7,2,7,7,6,6,2,6,4))
plot(g)

我想提取一个边缘列表,该列表由连接不同类型(雄性和雌性)的节点的边缘组成:

edgelist <- rbind(c(3,7),
               c(4,6),
               c(4,7),
               c(5,7))

assortativity使用连接M和F类型的顶点的边的分数,但是我不知道要明确提取这些边。

get.edgelist仅返回整个边缘列表,无法设置条件。

2 个答案:

答案 0 :(得分:5)

您可以使用"GET /personn?personID=123 HTTP/1.1" 404 - "-" "PostmanRuntime选择器来查找将公节点连接到母节点的边。例如

%--%

E(g)[V(g)[gender=="M"] %--% V(g)[gender=="F"]] 找到所有“公”节点,V(g)[gender=="M"]找到所有母节点,V(g)[gender=="F"]找到两组之间的所有边。

答案 1 :(得分:2)

edges = get.edgelist(g)
edges[rowSums(apply(edges, 2, function(x) get.vertex.attribute(g, "gender", x)) == "M") == 1,]
#     [,1] [,2]
#[1,]    4    7
#[2,]    3    7
#[3,]    5    7
#[4,]    4    6