How to create a bipartite network in R with igraph or tnet

帅比萌擦擦* 提交于 2019-11-27 08:47:30

In igraph a bipartite network is one that has a type vertex attribute. This attribute must be logical and must the TRUE for one of the node types and FALSE for the others. So to create a bipartite network from your edge list, you simply create a regular graph and then add the type vertex attribute:

edgelist <- read.table(text="Person    Event
                         Amy       football
                         Bob       picnic
                         Sam       artshow", 
                   header=TRUE)
igraph <- graph.data.frame(edgelist)

V(igraph)$type <- V(igraph)$name %in% edgelist[,1]
igraph
# IGRAPH DN-B 6 3 -- 
# + attr: name (v/c), type (v/x)

The 'B' letter tells you that this is a bipartite graph. You can create the unipartite projections of this network via:

bipartite.projection(igraph)
# $proj1
# IGRAPH UN-B 3 0 -- 
# + attr: name (v/c), type (v/x)
#
# $proj2
# IGRAPH UN-B 3 0 -- 
# + attr: name (v/c), type (v/x)

This will return a list of two graphs. If you think that the projection might be too big, you can first call the bipartite.projection.size function, this will give you the number of vertices and edges in both projections. The memory requirement for an igraph graph is (4m+2n)*8+O(1) bytes, where 'n' is the number of vertices and 'm' is the number of edges.

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