[R] anyone know how to combine two vector with some # overlaped?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 5 10:41:46 CEST 2004


On Wed, 5 May 2004, Richard A. O'Keefe wrote:

> If you want this:
> 
> 	> Suppose I have two vector say x=c(1 2 3 4 5) and y=(2
> 	> 3 6 7). Then I want to combine these two vector
> 	> together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only
> 	> appear once.
> 
> Julian Taylor <julian.taylor at adelaide.edu.au> suggests:
> 	x <- c(1,2,3,4,5)
> 	y <- c(2,3,6,7)
> 	z <- c(x,y)[!duplicated(c(x,y))] 
> 	
> But you can do it in one step:
> 	z <- unique(c(x,y))
> 
> I don't know how unique() is implemented, but using a hash table it
> _could_ be done in linear expected time, and in practice it seems to
> be pretty quick, more than quick enough for a few hundred elements.

It does use a hash table (as does duplicated).

An slightly shorter step is union(x, y), which is implemented as 
unique(c(x,y)).

-- 
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