title: Naming list items via loop in R
body: <p>I want to take a list, create a String vector of the names of the list items, filling in blanks with a generic name and then set the names vector as the names of the list. </p>
  
  <p>My code works fine for list who dont have items with names in it. It however does nothing, when there are items with a name in it.</p>
  
  <pre><code>addNamesToList &lt;- function(myList){
    listNames &lt;- vector()
    for(i in 1:length(myList)){
      if(identical(names(myList[i]),NULL)){
        listNames &lt;- c(listNames,paste("item",i,sep=""))
      }else{
        listNames &lt;- c(listNames,names(myList[i]))
      }
    }
    names(myList) &lt;- listNames
    return (myList)
  }
  </code></pre>
  
  <p>result without named items</p>
  
  <pre><code>$item1
  [1] 2 3 4
  
  $item2
  [1] "hey" "ho" 
  </code></pre>
  
  <p>result with named items</p>
  
  <pre><code>[[1]]
  [1] 2 3 4
  
  [[2]]
  [1] "hey" "ho" 
  
  $hello
  [1] 2 3 4
  </code></pre>
  
  <p>Hope you can help.</p>