R /网络分析 - 如何按节点的属性创建边

时间:2016-11-11 13:29:12

标签: r igraph vertices edges network-analysis

亲爱的Stackoverflow社区,

我目前正在使用R编译一个联盟网络,其中节点是公司/伞形组织,并且联系被定义为“成员”。目前,我的列表仍然很小,我可以根据节点的位置创建边缘(我使用igraph):

g <- igraph::add_edges(g, c(1,51,
                          1,52,
                          1,53,
                          1,54))

但是,我正在添加新节点,最终网络将包括至少500个组织。这意味着每次添加新节点时,节点的位置都会发生变化。由于每次添加新节点时都无法重做边缘,有没有一种方法可以添加知道节点名称的边缘?

节点的名称被视为属性,我试图使用与上面相同的命令,包括名称 - 而不是位置 - 但它不起作用:

g <- igraph::add_edges(g, c(V(g)$name=="Company1", V(g)$name == "Umbrella2"))

关于如何通过指定名称而不是位置来创建边缘的任何建议?

1 个答案:

答案 0 :(得分:0)

我相信你正在寻找as.numeric(V(g)["Company1"])

我强烈建议反对在 R-script中构建您的网络结构。即使对于小型网络,我也会在excel文件中输入数据,创建一个R脚本,将数据作为边缘列表读取并从中创建igraph。这样,您可以随时添加您的公司和组织,更好地监控实际进入您网络的数据,我想这首先是您正在寻找的内容。尽管如此,在这里做到这一点将超出范围。

至于按节点添加节点,我为你写了这个例子,我希望这是教学法。

library(igraph)

# Make an empty Bipartite graph
g <- make_bipartite_graph(0, NULL, directed=TRUE)
g <- delete_vertices(g, 1)


# Create vertices of two different types: companies and umbrellas
g  <- add_vertices(g, 5, color = "red", type=TRUE, name=paste("Company", 1:5, sep="_"))
g  <- add_vertices(g, 2, color = "blue", type=FALSE, name=paste("Umbrella", 1:2, sep="_"))

# In a bipartate graph edges may only appear BETWEEN verticies of different types. Companies
# can belong to umbrellas, but not to each other.

# Look at the types:
ifelse(V(g)$type, 'Company', 'Umbrella')  # true for companies, false for umbrellas

# Lets add some edges one by one. This is what I believe you're asking for in the question:
g <- add_edges(g, c(as.numeric(V(g)["Company_1"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_1"]), as.numeric(V(g)["Umbrella_2"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_2"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_3"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_4"]), as.numeric(V(g)["Umbrella_2"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_5"]), as.numeric(V(g)["Umbrella_2"])))

# Note that "Company_1" belongs to two umbrella organisations, as I assume your companies can:
plot(g)