Hi Rui:

Good points. However, as my offering is, I think, little more than a
disguised loop -- as compared to yours' and Ted's truly vectorized
solutions --I think it is far too inefficient to useful. I offered it
merely for its "aesthetic" interest, which is what you noted in your
comments below. Your point about using an anonymous function instead of
"foo" is right on, though.

Best,
Bert

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

> Hello,
>
> The two approaches are conceptually different. With yours we get equality
> given a certain number of (significant) digits, which might be what you are
> looking for if the measurements are known to be made with those digits, a
> frequent case.
>
> And there's also Bert's Vectorize way, with the bennefit of taking
> advantage of all.equal's behavior for 'scale' and for absolute vs. relative
> differences. It seems cleaner to rewrite it using an unnamed function
> instead of defining foo.
>
> Rui Barradas
>
> Em 01-09-2012 15:50, (Ted Harding) escreveu:
>
>> Thanks, Rui, but that was not my (apparently implicit) question.
>> Apologies if my wording gave the wrong impression. I was really
>> asking if such a function was already available somewhere in R.
>>
>> Yes, your definition of equals() is one possibility; or, with the
>> work-round I was using,
>>
>>    equals <- function(x,y,dig=4){round(x,**dig)==round(y,dig)}
>>
>> Best wishes,
>> Ted.
>>
>> On 01-Sep-2012 14:20:26 Rui Barradas 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.
>>>
>> ------------------------------**-------------------
>> E-Mail: (Ted Harding) <Ted.Harding@wlandres.net>
>> Date: 01-Sep-2012  Time: 15:50:18
>> 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.
>>
>
>


-- 

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]]


