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

Robin Evans rje42 at stat.washington.edu
Wed Apr 7 23:44:06 CEST 2010


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?

Thanks,

Robin

--
Robin Evans
Statistics Department
University of Washington
www.stat.washington.edu/~rje42



More information about the R-help mailing list