2016-11-11 108 views
0

亲爱的#1的社区,我目前使用R键编译归属网络中节点企业/伞状组织和关系被定义为“会员”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")) 

如何我任何建议可以通过指定名称而不是位置创建边?

回答

0

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

我强烈建议针对建立您的网络结构一个R脚本,但。即使对于一个小型网络,我也会将我的数据输入到excel文件中,创建一个R-script,将数据作为边界列表读取并从中创建一个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) 
+0

谢谢,这非常有用。该命令处理了我当前的数据,但我同意我应该先编译Excel电子表格。 –

相关问题