Sorting is unnecesssary. If column order is unimportant, identical() is unnecessary.
foo <- function(x,lookfor)apply(x,1,function(x)all(lookfor %in% x)))) will do. If column order is important, apply(x,1,identical,lookfor) will do. Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Scott Sent: Saturday, March 27, 2010 8:36 PM To: Peter Ehlers Cc: r-help@r-project.org; Berend Hasselman Subject: Re: [R] Matching pairs of values Many thanks Peter and Berend. I think I can make this work for my problem. Apologies for not giving an example: I was a bit tired and frustrated when I posted to R-help and only on later reflection realised that I didn't really follow the posting guidelines. David Peter Ehlers wrote: > (Sorry, I think that I just hit a wrong button and managed > to send a non-reply.) > > I think that David might have meant that the column order of the > numbers in lookfor is unimportant. In that case, a simple fix would > be either to check both lookfor and rev(lookfor) (since the > matrix is nX2) or to sort before testing: > > vtest <- function(x, lookfor){ > any(apply(x, 1, function(v) > {identical(sort(v), sort(lookfor))}))} > > -Peter Ehlers > > > On 2010-03-27 2:46, Berend Hasselman wrote: >> >> 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 > -- _________________________________________________________________ David Scott Department of Statistics The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055 Email: d.sc...@auckland.ac.nz, Fax: +64 9 373 7018 Director of Consulting, Department of Statistics ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.