[R] Matching pairs of values

Berend Hasselman bhh at xs4all.nl
Sat Mar 27 15:33:32 CET 2010



Peter Ehlers wrote:
> 
> 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))}))}
> 

Indeed, I never thought about that.
So to avoid too much sorting of lookfor I would propose this

vtest <- function(x, lookfor, sorted=FALSE) {
    if( sorted ) {
        lookfor <- sort(lookfor)
        any(apply(x,1, function(v) identical(sort(v),lookfor)))
    }
    else
        any(apply(x,1, function(v) identical(v,lookfor)))
}

Of course the default for sorted can be changed.

Berend


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



More information about the R-help mailing list