title: Is it possible to modify list elements?
body: <p>I have a list of records:</p>
  
  <pre><code>z &lt;- list(list(a=1),list(a=4),list(a=2))
  </code></pre>
  
  <p>and I try to add fields to each of them.
  Alas, neither</p>
  
  <pre><code>lapply(z,function(l) l$b &lt;- 1+l$a)
  </code></pre>
  
  <p>nor</p>
  
  <pre><code>for(l in z) l$b &lt;- 1+l$a
  </code></pre>
  
  <p>modifies <code>z</code>.  </p>
  
  <p>In this simple case I can, of course, do</p>
  
  <pre><code>z &lt;- lapply(z,function(l) c(list(b= 1+l$a),l))
  </code></pre>
  
  <p>but this quickly gets out of hand when the lists have more nesting:</p>
  
  <pre><code>z &lt;- list(list(a=list(b=1)),list(a=list(b=4)),list(a=list(b=2)))
  </code></pre>
  
  <p>How do I turn it into</p>
  
  <pre><code>list(list(a=list(b=1,c=2)),list(a=list(b=4,c=5)),list(a=list(b=2,c=3)))
  </code></pre>
  
  <p>without repeating the definition of the whole structure?
  Each element of <code>z</code> has many fields, not just <code>a</code>; and <code>z[[10]]$a</code> has many subfields, not just <code>b</code>.</p>