【发布时间】:2014-01-23 23:29:07
【问题描述】:
我使用 igraph 库在 R 中创建了一个无向的 Erdos-Renyi 网络。它有 100 个节点,p=0.2:
library(igraph)
original <- erdos.renyi.game(100, 0.2, type = c("gnp", "gnm"), directed = FALSE,
loops = FALSE)
我还创建了两个空网络:
net1 <- graph.empty(100)
net2 <- graph.empty(100)
我根据生成的随机数(0-1 之间)从原始网络向 net1 和 net2 添加边。如果这个随机数在0-0.1之间,边进入net1,如果在0.1-0.9之间,边进入net2,如果在0.9-1之间,边进入net1和net2。
这是我的代码,用于查看原始网络中的所有边缘并将它们添加到 net1、net2 或两者中。
i <- 1
while (get.edge(original, E(original)[i])) { #looks through all nodes in original
#network
# to generate a random number
randnum <- runif(1, min=0, max=1)
#to put the edge in net1, net2 or both
head <- get.edge(original, E(original)[i])[1]
tail <- get.edge(original, E(original)[i])[2]
if (randnum >= 0 && randnum < 0.1) {
net1 <- add.edges(net1, c(tail, head)) #puts edge in net1
} else if (randnum >= 0.1 && randnum < 0.9) {
net2 <- add.edges(net2, c(tail, head)) #puts edge in net2
} else if (randnum >= 0.9 && randnum <= 1) {
net1 <- add.edges(net1, c(tail, head)) #puts edge in net1
net2 <- add.edges(net2, c(tail, head)) #puts edge in net2
}
i <- i + 1
}
使用上面的代码,我不断收到以下错误消息:
Error in if (id < 1 || id > ec) { : missing value where TRUE/FALSE needed
这个警告信息,多次,因为它通过'while'循环:
In while (get.edge(original, E(original)[i])) { :
the condition has length > 1 and only the first element will be used
我不太清楚为什么会收到错误和警告消息,或者如何修复它们。
任何帮助将不胜感激。
【问题讨论】:
标签: r networking random graph igraph