Robin Evans
2010-Apr-07 21:44 UTC
[R] Vectorized forms of isTRUE, identical and all.equal?
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
Steve Lianoglou
2010-Apr-07 23:12 UTC
[R] Vectorized forms of isTRUE, identical and all.equal?
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? -steve> > Thanks, > > Robin > > -- > Robin Evans > Statistics Department > University of Washington > www.stat.washington.edu/~rje42 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact