[R] multiple logical comparisons

Martin Batholdy batholdy at googlemail.com
Sun Mar 21 22:57:07 CET 2010


thanks!



Now I have one more question;

How can I do the reverse?
when %in% is == (for two vectors of different lengths); what is the equivalent to !=  ?



On 21.03.2010, at 22:33, Erik Iverson wrote:

> Martin Batholdy wrote:
>> Hi,
>> I would like to compare a column of data with a vector.
>> I have this data.frame for example;
>> x <- data.frame(A = c(1:5), B = c(1,1,2,2,2))
>> Now I have a search vector:
>> search <- c(1,3,5)
>> when I now try to get all the data-rows which have a 1, a 3, or a 5 in column A and a 2 in column B, I tried this:
>> x[x$B == 2 & x$A == search,]
>> I hoped to get 3 2
>> 5 2
>> as output.
> 
> See ?%in%
> 
> x[x$B == 2 & x$A %in% search, ]
> 
> 



More information about the R-help mailing list