title: Rename portions of named list of lists without for loop
body: <p>I have a list of lists like this</p>
  
  <pre><code>list1 &lt;- list(a = 1, error = 2)
  list2 &lt;- list(a = 1, error = 3)
  mylist &lt;- list(list1, list2)
  </code></pre>
  
  <p>I'd like to rename all the elements currently named <code>"a"</code> to the names in <code>new_names</code></p>
  
  <pre><code>new_names &lt;- c("new_1", "new_2")
  </code></pre>
  
  <p>I can do this with a <code>for</code> loop like so</p>
  
  <pre><code>for(i in seq_along(mylist)) {
    names(mylist[[i]])[names(mylist[[i]]) == "a"] &lt;-  new_names[i]}
  </code></pre>
  
  <p>and get my desired result:</p>
  
  <pre><code>mylist
  [[1]]
  [[1]]$new_1
  [1] 1
  
  [[1]]$error
  [1] 2
  
  
  [[2]]
  [[2]]$new_2
  [1] 3
  
  [[2]]$error
  [1] 4
  </code></pre>
  
  <p>I'd like to do this without the <code>for</code> loop.  Something like this <code>map</code> function perhaps, which doesn't work because it replaces all names, not just those that are <code>"a"</code>.</p>
  
  <pre><code>library(purrr)
  mylist_2 &lt;- map(mylist, ~{names(.)[names(.) == "a"] &lt;-  new_names})
  </code></pre>