[R] Vectorized forms of isTRUE, identical and all.equal?

Robin Evans rje42 at stat.washington.edu
Thu Apr 8 01:16:18 CEST 2010

On 7 April 2010 16:12, Steve Lianoglou <mailinglist.honeypot at gmail.com> wrote:
> Hi,
> On Wed, Apr 7, 2010 at 5:44 PM, Robin Evans <rje42 at stat.washington.edu> wrote:
>> Dear all,
>> I'm wondering if there exist vectorized forms of 'isTRUE()',
>> 'identical()' and 'all.equal()'.  My problem is that I wish to test if
>> each element of a vector is equal to a particular value (or
>> numerically close), whilst dealing carefully with NAs and so on.
>> However, using sapply() with identical() is very slow because it makes
>> so many separate function calls:
>> x = rbinom(1e4, 1, 0.5)
>> system.time(sapply(x, function(x) isTRUE(all.equal(x, 0))))
>> system.time(abs(x)  < .Machine$double.eps^0.5)
>> The latter version is fast, but potentially dangerous.  Any suggestions?
> Why is it dangerous? Because some values in x can be NA?
Precisely - I would like all the answers to be TRUE or FALSE.

Robin Evans
Statistics Department
University of Washington

More information about the R-help mailing list