I have two list with exactly the same number of elements. I want to copy the names from one list to another.
            Asked
            
        
        
            Active
            
        
            Viewed 1,252 times
        
    0
            
            
        - 
                    I fear I don't understand what you're trying to do. Please provide a reproducible example... – juba Sep 23 '13 at 13:07
- 
                    As of now, your question seems too unclear to answer properly. Please give us a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of what you're trying to do, and some code for what you've tried. – Blue Magister Sep 23 '13 at 13:32
- 
                    Roland solution seems to answer your problem. But (even if you didn't provide us with so much info) I have the feeling that it'd be worth for you spending a bit of time in learning OOP in R. This seems a case where you have several objects all with same structure. To begin with have a look to S3 classes which are in essence lists with class attributes. Creating, say, new "foo" S3 objects will remove the names problem I think – Michele Sep 23 '13 at 13:33
- 
                    Yes! it's working... and of course I'll have to learn a lot! Thank you!! – user2794659 Sep 23 '13 at 14:07
1 Answers
2
            
            
        Use unlist/relist for nested lists:
a <- list(x=1, y=2, z=list(foo=1:5))
b <- list(p="a", q="b", r=list(bar=1:5))
copyNames <- function(l1, l2) {
  relist(unlist(l1), l2)
}
copyNames(a,b)
This assumes that you really have the exact same number of elements in the list and all vectors etc. inside the list.
 
    
    
        Roland
        
- 127,288
- 10
- 191
- 288
 
    