[R] Department of Redundancy Department.

Henrik Bengtsson hb at stat.berkeley.edu
Fri Aug 15 02:00:00 CEST 2008


Hmm,

couldn't resists:

> X <- NA
> is.logical(X)
[1] TRUE
> (X == TRUE)
[1] NA

> "==.MaybeNA" <- function(e1, e2) { !is.na(e1) && (e1 == e2) }
> X <- structure(NA, class="MaybeNA")
> is.logical(X)
[1] TRUE
> (X == TRUE)
[1] FALSE

Ta da ;)

Henrik

PS. It might be worth mentioning base::isTRUE() when we're already at it. DS.


On Thu, Aug 14, 2008 at 3:07 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
>
> I just noticed a certain ``usage'' in a recent posting, and couldn't
> restrain my self from commenting.  The usage was of the form
>
>        ``if(X==TRUE)''
>
> where X was a logical variable.
>
> This sort of thing is brought to you by your Department of Redundancy
> Department.  The ``==TRUE'' bit is irrelevant, incompetent, and immaterial,
> as Perry Mason used to say.  The value of X==TRUE is exactly the same as
> the value of X when X is a logical variable.  If X is not a logical variable
> a lot of strange things can happen, depending on the nature of X.
>
> It is safer and takes 6 fewer key strokes to use ``if(X)''.  So why not
> do that, eh?
>
>        cheers,
>
>                Rolf Turner
>
> P. S.  The example that triggered this post actually used ``if(X==T)''.
> This is doubly dangerous since ``T'' can be overwritten.  If it was
> overwritten you'd get toadally wrong answers.  E.g., suppose you had
> set
>
>        T <- FALSE   # !!!
>
> ***ALWAYS USE ``TRUE'' AND ``FALSE'' FOR LOGICAL VALUES.
>   DO NOT USE ``T'' AND ``F''.***
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list