Naming list elements in R
<p>I've been doing some work with some large, complex lists lately and I've seen some behaviour which was surprising (to me, at least), mainly to do with assigning names to a list. A simple example:</p>

<pre><code>Fil &lt;- list(
a = list(A=seq(1, 5, 1), B=rnorm(5), C=runif(5)), 
b = list(A="Cat", B=c("Dog", "Bird"), C=list("Squirrel", "Cheetah", "Lion")),
c = list(A=rep(TRUE, 5), B=rep(FALSE, 5), C=rep(NA, 5)))

filList &lt;- list()

for(i in 1:3){
  filList[i] &lt;- Fil[i]
  names(filList)[i] &lt;- names(Fil[i])
}
identical(Fil,filList)
[1] TRUE
</code></pre>

<p>but:</p>

<pre><code>for(i in 1:3){
  filList[i] &lt;- Fil[i]
  names(filList[i]) &lt;- names(Fil[i])
}
identical(Fil,filList)
[1] FALSE
</code></pre>

<p>I think the main reason it confuses me is because the form of the left-hand side of first <code>names</code> line in the first for loop needs to be different from that of the right-hand side to work; I would have thought that these should be the same. Could anybody please explain this to me?</p>