title: Append an object to a list in R in amortized constant time, O(1)?
body: <p>If I have some R list <code>mylist</code>, you can append an item <code>obj</code> to it like so:</p>
  
  <pre><code>mylist[[length(mylist)+1]] &lt;- obj
  </code></pre>
  
  <p>But surely there is some more compact way.  When I was new at R, I  tried writing <code>lappend()</code> like so:</p>
  
  <pre><code>lappend &lt;- function(lst, obj) {
      lst[[length(lst)+1]] &lt;- obj
      return(lst)
  }
  </code></pre>
  
  <p>but of course that doesn't work due to R's call-by-name semantics (<code>lst</code> is effectively copied upon call, so changes to <code>lst</code> are not visible outside the scope of <code>lappend()</code>.  I know you can do environment hacking in an R function to reach outside the scope of your function and mutate the calling environment, but that seems like a large hammer to write a simple append function.</p>
  
  <p>Can anyone suggest a more beautiful way of doing this? Bonus points if it works for both vectors and lists.</p>