Yes, Rui's suggestion is what I would do, too.

However, for fun, here is a different, way less efficient, but perhaps
instructive (or amusing?) alternative using Vectorize() and all.equal()
directly:

> foo <- function(target,current,...)isTRUE(all.equal(target,current,...))
> equals <- Vectorize(foo,"current")
> x <- runif(10)
> equals(.7,x)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> equals(.7,x,tol=.1)
 [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> equals(.7,x,tol=.2)
 [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE

Perhaps a bit more interesting is another such possibility suggested by
Ted's query: How to convert match() to to a function matchWithTolerance(),
say, that matches a numeric vector x with a target vector y, where match
means equality within a tolerance. An issue here is that a single x might
match several y's. But in this case, since it seems a  bit less trivial
than Ted's, I would certainly suspect that there are already several
solutions available (perhaps via clustering) if one looked.

Cheers,
Bert


On Sat, Sep 1, 2012 at 7:20 AM, Rui Barradas <ruipbarradas@sapo.pt> wrote:

> Hello,
>
> Like this?
>
> equals <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol
>
>
> x <- rnorm(5)
> y <- x[2]
> equals(x, y)
> [1] FALSE  TRUE FALSE FALSE FALSE
>
> y <- rnorm(3)
> y[2] <- x[2]
> equals(x, y)
> [1] FALSE  TRUE FALSE FALSE FALSE
> Warning message:
> In x - y : longer object length is not a multiple of shorter object length
>
> Hope this helps,
>
> Rui Barradas
> Em 01-09-2012 14:48, (Ted Harding) escreveu:
>
>> Greetings All.
>>
>> Once again, I am probably missing something fairly accessible,
>> but since I can't find it I'd welcome advice!
>>
>> I have a dataframe derived from a text file of data in tabular
>> format. For one of the variables, say X, I want to select the
>> subsets which in which X equals a particular value.
>>
>> Such values are given in the text file like: 2.3978953, and each
>> such value will occurr several times. So, for example, I want
>> to select that subset of rows for which X "equals" 2.3978953 .
>>
>> However, "==" of course will not do, because it is intolerant of
>> rounding errors. Nor will all.equal(), which does have tolerance,
>> since it only returns a single TRUE/FALSE (and in any case will
>> not compare a vector with a value. But the only guidance I can
>> find for this situation, using ?"==", says:
>>
>>    For numerical and complex values, remember '==' and ?!=? do not
>>    allow for the finite representation of fractions, nor for rounding
>>    error. Using 'all.equal with 'identical is almost always preferable.
>>    See the examples.
>>
>> But the only relevant example is:
>>
>>    x1 <- 0.5 - 0.3
>>    x2 <- 0.3 - 0.1
>>    x1 == x2                           # FALSE on most machines
>>    identical(all.equal(x1, x2), TRUE) # TRUE everywhere
>>
>> which is not a vectorial example.
>>
>> I do have a work-round of the form:
>>
>>    ix <- which(round(X,4)==round(2.**3978953,4))
>>
>> (where rounding to 4 decimal places is enough to distinguish X-values).
>>
>> As said above,
>>
>>    ix <- which(X==2.3978953)
>>
>> will not do, since 'which(X==x)' returns an empty result. And using
>> all.equal(X,2.3978953) fails because X and 2.3978953 are of unequal
>> lengths.
>>
>> So it would be nice if there were a function (which I can of course
>> define for myself), say equals(), such that
>>
>>    equals(X,2.3978953)
>>
>> (with optional "tol=" argument) would test for equality to within
>> tolerance, and return a vector of TRUE/FALSE according to the values
>> in X.
>>
>> Or, indeed, perhaps I am overlooking something.
>>
>> Ted.
>>
>> ------------------------------**-------------------
>> E-Mail: (Ted Harding) <Ted.Harding@wlandres.net>
>> Date: 01-Sep-2012  Time: 14:48:01
>> This message was sent by XFMail
>>
>> ______________________________**________________
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <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<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

	[[alternative HTML version deleted]]


