[R] set differences (was Logical "and")

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 11 14:47:51 CET 2004


The answer is the function setdiff(), but I suppose you have to know what
the operation is called to find it.

> setdiff(a, b)
[1]  1  5 11

You may find it illuminating to see how it is implemented.

On Thu, 11 Nov 2004, Alexander Sokol wrote:

> Hello,
>
> I have the following very simple problem:
>
> Say I have two vectors,
>
> a<-c(1,7,4,5,9,11)
> b<-c(7,4,9)
>
> I would like to create a vector containing the elements in a which are not in
> b.
>
> Obviously, this is possible by writing
>
> a[a!=b[1] & a!=b[2] & a!=b[3]]
>
> But I would like a solution which is applicable to the situation where the
> number of elements in b is unknown.
>
> I have looked in the R manuals, the FAQ and the mailing lists, but have been
> unable to find a solution.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list