[R] Unique lists from a list

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Sep 1 16:49:00 CEST 2004


On Wed, 1 Sep 2004, michael watson (IAH-C) wrote:

> 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
> "Address".
> 
> 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?

match, as in match(unique(list$Name), list$name), OR indexing as in

Address <- list$Address
names(Address) <- list$Name
Name <- unique(list$Name)
list(Name, as.vector(Address[Name])

OR choose a better data structure as in

unique(as.data.frame(list))

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list