Search code examples
rmatrixbipartite

generating random matrixes with genweb in a for loop


I´m an R beginner and I couldn't find anything that helped me. I want to generate random matrices, that differ in size. i wanted to use a for-loop and the genweb function, but instead of many matrices it gives me back a vector.

library(bipartite)
zeroo <-matrix()
for(i in 10:50){
   zeroo[i]<-genweb(10, i, dens=2) 
  }

Solution

  • Your for loop can be changed

    col1 <- 10:50
    zeroo <- vector('list', length(col1)) #create a `list`
    for(i in seq_along(col1)){
       zeroo[[i]] <- genweb(10, col1[i], dens=2) #
    }
    
    length(zeroo)
    #[1] 41
    
    zeroo[[1]]
    #     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    #[1,]    5    5    2    6    0    0    3    1   15     2
    #[2,]    2    2    0    1    1    0    0    0    2     0
    #[3,]    5    3    2    1    1    0    1    0    3     1
    #[4,]    3    3    3    2    1    0    0    2    6     5
    #[5,]    2    7    1    5    0    0    2    0    5     8
    #[6,]    7    3    2    1    1    0    2    0    9     0
    #[7,]    1    0    1    1    0    1    0    0    1     0
    #[8,]    4    2    3    6    0    0    0    0    6     2
    #[9,]    3    0    0    2    0    0    0    0    0     0
    #[10,]   6    3    2    4    1    2    0    0    3     2