[R] Matching pairs of values

Berend Hasselman bhh at xs4all.nl
Sat Mar 27 09:46:53 CET 2010



David Scott-6 wrote:
> 
> I am sure someone can come up with a clever way of doing what I want---I 
> don't seem to be able to.
> 
> I want to check if a pair of numbers occurs as one of the rows of an n 
> by 2 matrix. If I was only checking whether a single number was in a 
> vector of numbers I would use %in% but I can't see how to generalize to 
> this case.
> 

Would this help?

vtest <- function(x, lookfor) any(apply(x,1, function(v)
identical(v,lookfor)))

> ma <- matrix(c(2,3,1,5,7,3),ncol=2)
> ma
     [,1] [,2]
[1,]    2    5
[2,]    3    7
[3,]    1    3
> vtest(ma,c(3,7))
[1] TRUE
> vtest(ma,c(1,7))
[1] FALSE

Berend
-- 
View this message in context: http://n4.nabble.com/Matching-pairs-of-values-tp1693151p1693196.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list