Search code examples
loopsnested-loopsigraph

create multiple prefential attachment igraphs with increments on pa.exp


I am trying to create multiple igraphs so far I only have: `

set.seed(1)
   gs <- list()
   for (x in seq_len(100L)) {
   gs[[x]] <- sample_pa_age(10000, pa.exp=1.05, aging.exp=0.0, aging.bin=1000)
   E(gs[[x]])$weight <- sample(1:5, ecount(gs[[x]]), T)
   }
   plot(gs[[1]], edge.width = E(gs[[1]])$weight) # plot first graph

` BUT what I want to do is to change pa.exp from=1 to=3 with an increment of .05., generating multiple graphs. What to do?


Solution

  • Using a for loop:

    pa <- seq(1, 3, 0.05)
    gs <- vector("list", length(pa))
    i <- 1
    for(x in pa){
      gs[[i]] <- sample_pa_age(10000, pa.exp=x, aging.exp=0.0, aging.bin=1000)
      E(gs[[i]])$weight <- sample(1:5, 9999, T)
      i <- i+1
    }
    

    Alternatively, you can use lapply, so that you dont have to create the list beforehand:

    my_graph <- function(x, n=10000){
      g <- sample_pa_age(n, pa.exp=x, aging.exp=0.0, aging.bin=1000)
      E(g)$weight <- sample(1:5, n-1, T)
      return(g)
    }
    gs <- lapply(seq(1, 3, 0.05), my_graph)