[R] Unique lists from a list

james.holtman@convergys.com james.holtman at convergys.com
Wed Sep 1 17:01:54 CEST 2004

Try this:

l.1 <- list(list(name='a', addr='123'),list(name='b', addr='234'),
      list(name='b', addr='234'), list(name='a', addr='123'))  # create a

l.names <- unlist(lapply(l.1, '[[', 'name'))  # get the 'name'
l.u <- unique(l.names)  # make unique

new.list <- l.1[match(l.u, l.names)]      # create new list with just one

James Holtman        "What is the problem you are trying to solve?"
Executive Technical Consultant  --  Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929

                      "michael watson                                                                                                     
                      (IAH-C)"                     To:       <R-help at stat.math.ethz.ch>                                                   
                      <michael.watson at bbsrc        cc:                                                                                    
                      .ac.uk>                      Subject:  [R] Unique lists from a list                                                 
                      Sent by:                                                                                                            
                      r-help-bounces at stat.m                                                                                               
                      09/01/2004 10:31                                                                                                    


I have a list.  Two of the elements of this list are "Name" and
"Address", both of which are character vectors.  Name and Address are
linked, so that the same "Name" always associates with the same

What I want to do is pull out the unique values, as a new list of the
same format (ie two elements of character vectors).  Now I've worked out
that unique(list$Name) will give me a list of the unique names, but how
do I then go and link those to the correct (unique) addresses so I end
up with a new list which is the same format as the rest, but now unique?


R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list