title: Iteratively and efficiently adding elements to a list attribute of vertices in R igraph
body: <p>I'm working on a signal-propagation algorithm in <strong>R</strong>, using <strong>igraph</strong> (a library for random graphs) which involves working with a 2-level nested list.</p>
  
  <p>Igraph allows to attach attributes to vertices (nodes of the graph), these can be vectors or lists, butin my application I need nested lists.</p>
  
  <p>To see, try:</p>
  
  <pre><code>library("igraph")
  g &lt;- graph.full(10) # create a fully connected graph with 10 vertices
  V(g)$letters &lt;- list(NULL) # adds a list called "letters" to every vertex
  V(g)$letters # results in a nested list
  </code></pre>
  
  <p><em>I would like to add, in different stages, some pre-determined elements stored in a vector to a given subset of 2nd-level lists, where the subsetted list is the same size as the vector.</em></p>
  
  <p>The problem is to find an efficient way to add elements to the 2nd-level lists.</p>
  
  <p>The simpler (and so far only) way to go is to write a loop:</p>
  
  <pre><code>set.seed(1234)
  
  # every iteration represents a "round" of element addition ,
  # followed by other operations. 
  # So the attribute "letters" should not be populated in one sitting.
  for (i in 1:10){
  
    # select randomly five 2nd-level lists (vertices) from the 1st-level list
    # the selected vertices are generated randomly for exposition, 
    # but I need to be able to select them from a well-defined vector (sel.ver)
  
    sel.vert &lt;- sample(1:10, 5)
  
    # generate elements to add to the lists in the 2nd-level list (vertices)
    # again, i generate them randomly just to fill the vector, 
    #but the vector could be pre-determined
  
    add.elem &lt;- sample(letters, 5)
  
    # now add add each element to its own list
    # notice that the first ELEMENT of add.elem (add.elem[1]) is added
    # to the attribute of the first SELECTED vertex (V(g)[sel.vert[1]]$letters,
    # the second element of add.elem with the second SELECTED vertex, and so on..
  
    for (l in 1:5){
      V(g)[sel.vert[l]]$letters &lt;- list(c(V(g)[sel.vert[l]]$letters, add.elem[l]))    
    }
  }
  </code></pre>
  
  <p>(I apologise to the experienced reader if this was a horror show of bad programming practices) </p>
  
  <p>As the size of the initial network grows larger and more vertices are selected at every iteration (a random number, instead of 5), the loops become much slower. This should be a "workhorse" function, so I would like to speed it up.</p>
  
  <p>I read the answer given to " <a href="https://stackoverflow.com/questions/2778510/efficiently-adding-or-removing-elements-to-a-vector-or-list-in-r">Efficiently adding or removing elements to a vector or list in R?</a> " , namely, working with vectors whenever possible and preallocating their size, but I think it doesn't apply to my case, because:</p>
  
  <ol>
  <li>I think that with <strong>igraph</strong> I have no choice but to use lists (at least at the first level)</li>
  <li>at the second level, the lists will have different final lengths, depending on which vertices are randomly selected. So, it is difficult to preallocate a vector of the correct size. Even if I put very large vectors at the second level, initially filled with NAs (resulting in a list of vectors), I wouldn't know in which position to add elements (because the length of the list at any iteration is random), not to mention that I would need to remove NAs later.</li>
  </ol>
  
  <p>This should be a particular case of adding elements (working with) nested list. As such,  I would think that maybe a faster implementation could be achieved by replacing the inner loop with <code>ddply</code> in <em>plyr</em> or <code>do.call</code>, but I couldn't manage to write the function to apply: <code>get the elements of the (inner) list and add this new element (itself a subset of a vector)</code></p>
  
  <p>Any comment or suggestion is appreciated. Hope the post is clear.</p>