Skip to content

Logical "and"

10 messages · Alexander Sokol, Sean Davis, Andy Bunn +7 more

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

Thank you for your replies,
 Alexander
#
On Nov 11, 2004, at 8:33 AM, Alexander Sokol wrote:

            
> a[!(a %in% b)]
[1]  1  5 11

Sean
#
How about this?

a<-c(1,7,4,5,9,11)
b<-c(7,4,9)
a[!a %in% b]

b<-c(7,4,9, 100, 20, 34, 54)
a[!a %in% b]

see ?match, too

HTH, Andy
#
Alexander Sokol <alexandersokol at ofir.dk> writes:
As in
[1]  1  5 11

or
[1]  1  5 11

?

(Note that they differ if a has nonunique values).
#
On Thu, 11 Nov 2004, Alexander Sokol wrote:
?setdiff
[1]  1  5 11

  
    
#
The answer is the function setdiff(), but I suppose you have to know what
the operation is called to find it.
[1]  1  5 11

You may find it illuminating to see how it is implemented.
On Thu, 11 Nov 2004, Alexander Sokol wrote:

            

  
    
#
Hi
On 11 Nov 2004 at 14:33, Alexander Sokol wrote:

            
[1] FALSE  TRUE  TRUE FALSE  TRUE FALSE
[1]  TRUE FALSE FALSE  TRUE FALSE  TRUE
[1]  1  5 11
Is it OK?

Cheers
Petr
Petr Pikal
petr.pikal at precheza.cz
#
Try

setdiff(a, b)

-roger
Alexander Sokol wrote:

  
    
#
Alexander Sokol wrote:

            
a[!(a %in% b)]

or see ?setdiff

Uwe Ligges
#
You can use setdiff if you only need the unique values of a that are not
in b. If you want all values you can use

a[a%in%setdiff(a,b)]

There are also intersection, union etc...
see
?setdiff
On Thu, 11 Nov 2004, Alexander Sokol wrote: