[Rd] isFALSE

William Dunlap wdunlap at tibco.com
Tue Oct 20 17:51:36 CEST 2009


> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Martin Maechler
> Sent: Tuesday, October 20, 2009 7:56 AM
> To: Romain Francois
> Cc: R-devel at r-project.org
> Subject: Re: [Rd] isFALSE
> 
> >>>>> "RF" == Romain Francois <romain.francois at dbmail.com>
> >>>>>     on Sun, 11 Oct 2009 01:00:24 +0200 writes:
> 
>     RF> Hello, Just wondering why there is "isTRUE" and not
>     RF> "isFALSE".
> 
> Probably because it's slightly more natural to have
> 
>   if(isTRUE(....)) {
>      ..
>   }
> 
> rather than the corrsponding  isFALSE()
> 
> Further, e.g.  all.equal()  and  validObject()
> return *either*  TRUE or a string,
> and hence isTRUE(.) is usefully applied to their return values.
> 
> Are there other opinions on this?

I avoid isTRUE because it is too restrictive, insisting
that the TRUE be unadorned with names or other stray attributes.
E.g.,
   > x<-c(One=1,Two=2)
   > isTRUE(x[1]>0) # <=> isTRUE(c(One=TRUE))
   [1] FALSE
I would avoid a similar isFALSE.  It would be nice to have
a isScalarTRUE function and a isScalarFALSE, both of which
would return FALSE for NA, non-scalars, and non-logicals.
Then I would use them in the form
    if (isScalarTRUE(x))
          doSomething()
    else if (isScalarFALSE(x))
          doSomethingElse()
    else stop("expected scalar TRUE or FALSE, not ", class(x), "(",
length(x), ")")
One might want to separate out the length(x)==1,is.logical,
and !is.na(x) tests to avoid duplicating computations, but that would
make
the if-else logic more complicated.

By the way, I sometimes use locally defined is.true() and is.false
functions that are vectorized and return FALSE for NA's:
   is.true <- function(x) x & !is.na(x)
   is.false <- function(x) !x & !is.na(x)
   is.true(c(NA,T,F))
   # [1] FALSE  TRUE FALSE
   is.true(c(NA,1,0))
   # [1] FALSE  TRUE FALSE
The idiom is in functions like ifelse().  Is there/should there be
a standard function to do that?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  
> 
> Martin
> 
>     > -- 
>     > Romain Francois
>     > Professional R Enthusiast
>     > +33(0) 6 28 91 30 30
>     > http://romainfrancois.blog.free.fr
>     > |- http://tr.im/BcPw : celebrating R commit #50000
>     > |- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
>     > `- http://tr.im/yw8E : New R package : sos
> 
>     > Index: src/library/base/R/identical.R
>     > 
> ===================================================================
>     > --- src/library/base/R/identical.R	(revision 50027)
>     > +++ src/library/base/R/identical.R	(working copy)
>     > @@ -18,3 +18,5 @@
>     > .Internal(identical(x,y, num.eq, single.NA, attrib.as.set))
>  
>     > isTRUE <- function(x) identical(TRUE, x)
>     > +
>     > +isFALSE <- function(x) identical(FALSE, x)
>     > Index: src/library/base/man/Logic.Rd
>     > 
> ===================================================================
>     > --- src/library/base/man/Logic.Rd	(revision 50027)
>     > +++ src/library/base/man/Logic.Rd	(working copy)
>     > @@ -14,6 +14,7 @@
>     > xor(x, y)
>  
>     > isTRUE(x)
>     > +isFALSE(x)
>     > }
>     > \alias{!}
>     > \alias{&}
>     > @@ -23,6 +24,7 @@
>     > \alias{xor}
>     > \alias{Logic}
>     > \alias{isTRUE}
>     > +\alias{isFALSE}
>     > \description{
>     > These operators act on logical and number-like vectors.
>     > }
>     > @@ -48,7 +50,11 @@
>     > \code{isTRUE(x)} is an abbreviation of 
> \code{identical(TRUE, x)}, and
>     > so is true if and only if \code{x} is a length-one 
> logical vector with
>     > no attributes (not even names).
>     > -
>     > +  
>     > +  \code{isFALSE(x)} is an abbreviation of 
> \code{identical(FALSE, x)}, and
>     > +  so is true if and only if \code{x} is a length-one 
> logical vector with
>     > +  no attributes (not even names).
>     > +  
>     > Numeric and complex vectors will be coerced to logical 
> values, with
>     > zero being false and all non-zero values being true.  
> Raw vectors are
>     > handled without any coercion for \code{!}, \code{&}, 
> \code{|} and
>     > @@ -87,7 +93,7 @@
>     > used.  For arrays (and an array result) the dimensions 
> and dimnames
>     > are taken from first argument if it is an array, 
> otherwise the second.
>  
>     > -  For \code{||}, \code{&&} and \code{isTRUE}, a 
> length-one logical vector.
>     > +  For \code{||}, \code{&&}, \code{isFALSE} and 
> \code{isTRUE}, a length-one logical vector.
>     > }
>     > \section{S4 methods}{
>     > \code{!}, \code{&} and \code{|} are S4 generics, the 
> latter two part
>     > Index: src/library/base/man/identical.Rd
>     > 
> ===================================================================
>     > --- src/library/base/man/identical.Rd	(revision 50027)
>     > +++ src/library/base/man/identical.Rd	(working copy)
>     > @@ -85,7 +85,8 @@
>     > \seealso{
>     > \code{\link{all.equal}} for descriptions of how two 
> objects differ;
>     > \link{Comparison} for operators that generate 
> elementwise comparisons.
>     > -  \code{\link{isTRUE}} is a simple wrapper based on 
> \code{identical}.
>     > +  \code{\link{isTRUE}} and \code{\link{isFALSE}} are simple 
>     > +  wrappers based on \code{identical}.
>     > }
>     > \examples{
>     > identical(1, NULL) ## FALSE -- don't try this with ==
>     > ______________________________________________
>     > R-devel at r-project.org mailing list
>     > https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



More information about the R-devel mailing list