[R] Logical "and"

Petr Pikal petr.pikal at precheza.cz
Thu Nov 11 14:47:53 CET 2004


Hi

On 11 Nov 2004 at 14:33, 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.

> a%in%b
[1] FALSE  TRUE  TRUE FALSE  TRUE FALSE
> !a%in%b
[1]  TRUE FALSE FALSE  TRUE FALSE  TRUE
> a[!a%in%b]
[1]  1  5 11
>

Is it OK?

Cheers
Petr

> 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list