Subset igraph graph by label

耗尽温柔 提交于 2019-12-05 11:32:28

问题


I am trying to subset a igraph graph by an edge characteristics (like its label). In the reproducible example I have shamelessly stolen from another post with a little modification, I would like to be able to separate the Best Friend ties (BF) from the Family ties (FAM):

edges <- matrix(c(103, 86, 24, 103, 103, 2, 92, 103, 87, 103, 103, 101, 103, 44), ncol=2, byrow=T)
g <- graph(as.vector(t(edges)))
E(g)[c(2:4,7)]$label<-"FAM"
E(g)[c(1,5,6)]$label<-"BF"

The best I can do so far is display the edges which have one type of tie:

E(g)[E(g)$label=="BF"]
V(g)[E(g)$label=="BF"]

回答1:


how about:

gfam <- subgraph.edges(graph=g, eids=which(E(g)$label=="FAM"), delete.vertices = TRUE)
gbf <- subgraph.edges(graph=g, eids=which(E(g)$label=="BF"), delete.vertices = TRUE)

Suggestion for igraph/network analysis tutorial/shameless plug: http://sna.stanford.edu/rlabs.php




回答2:


I suggest you read ?V and ?E to see how to select edges and vertices. A quite compact and readable solution to your question is

subgraph.edges(g, E(g)[label=="FAM"])
subgraph.edges(g, E(g)[label=="BF"])

This removes the vertices as well, if they don't have an incident edge of the specified label. See ?subgraph.edges for details.



来源:https://stackoverflow.com/questions/15857718/subset-igraph-graph-by-label

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