Warrior
Warrior

Reputation: 170

Creating edges between nodes in a nework in R

I have created an undirected Erdos-Renyi network in R using the igraph library. It has 100 nodes and p=0.2:

library(igraph)

original <- erdos.renyi.game(100, 0.2, type = c("gnp", "gnm"), directed = FALSE,
    loops = FALSE)

I have also created two empty networks:

net1 <- graph.empty(100)
net2 <- graph.empty(100)

I add edges to net1 and net2, from the original network, based on a random number (between 0-1) that is generated. If this random number is between 0-0.1, the edge goes in net1, if it is between 0.1-0.9, the edge goes in net2, and if it is between 0.9-1, the edge goes in both net1 and net2.

Here is my code to look through all of the edges in the original network and add them to either net1, net2 or both.

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
}

With the code above, I keep getting this error message:

Error in if (id < 1 || id > ec) { : missing value where TRUE/FALSE needed

And this warning message, multiple times, as it goes through the 'while' loop:

In while (get.edge(original, E(original)[i])) { :
    the condition has length > 1 and only the first element will be used

I'm not quite sure why I get the error and warning messages, or how to go about fixing them.

Any help would be much appreciated.

Upvotes: 1

Views: 551

Answers (1)

LoneWolf
LoneWolf

Reputation: 214

You could try using a for loop instead of a while loop:

for (i in min(E(original)):max(E(original))) { #looks through all the nodes in
                    #the original network from the minimum to the maximum edge
    # 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
    }
}

Using a for loop should get rid of the error and warning messages, and it will also be a lot more easier to do what you're trying to do here.

Upvotes: 1

Related Questions