[Rd] Use of all/any

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri, 26 Oct 2007 08:16:03 +0100 (BST)


all/any coerce their arguments to logical (if possible). I've added a warning in R-devel if coercion is from something other than integer.

This arose because it is easy to make a slip and write all(X) > 0 rather than all(X > 0): thanks to Bill Dunlap for bringing that to my attention. However, it has been useful in detecting quite a few other things:

                 iipiv[ii == 0] <- 0

which was intended to be

                 iipiv[ii == 0L] <- 0L

         all(lapply(z, is.numeric))

which is applying all() to a list. One might worry that

         sapply(z, is.numeric)

will return a list if length(z) == 0 (which it does) and so all() would warn, but that is covered by another change, to ignore all length-zero arguments (and so avoid the cost of coercion to logical(0)).

I decided not to warn on integer as it is so common. But at least some of these are thinkos. For example, constructions like

         all(grep(pattern, x))

occurred scores of times in the R sources. Since the value of grep() is an integer vector of positive indices, this is equivalent to

         length(grep(pattern, x)) > 0

and when used in a if() condition the '> 0' is not needed.

Some warnings are common from other packages: one is

Warning in any(textLocations) :

   coercing argument of type 'double' to logical

from lattice (and Deepayan Sarkar will fix that shortly). Quite a few others looked familiar but are the result of package authors copying code from base R or other packages: if you do that you do need to copy the bugfixes too.

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 26 Oct 2007 - 07:23:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 30 Oct 2007 - 03:46:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.