[R] Department of Redundancy Department.

Douglas Bates bates at stat.wisc.edu
Sat Aug 16 14:19:59 CEST 2008


On Fri, Aug 15, 2008 at 7:57 AM, Liaw, Andy <andy_liaw at merck.com> wrote:
> I couldn't resist, either...
>
>> From: Henrik Bengtsson
>>
>> 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 ;)
>
> But anything compared to NA should result in NA!
>
> Andy
>
>
>> Henrik
>>
>> PS. It might be worth mentioning base::isTRUE() when we're
>> already at it. DS.

The fact that isTRUE returns FALSE for an NA is one of the reasons
that one sometimes wants to use it.  There are occasions, such as
subscripting, when you only want the values that are not missing and
are logicals and are true.  Another occasion is in a phrase like
isTRUE(validObject(foo)) because validObject returns TRUE or a
character string describing the way in which the object failed to be
valid.

>> 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.
>> >
>>
>> ______________________________________________
>> 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.
>>
> Notice:  This e-mail message, together with any attachme...{{dropped:12}}
>
> ______________________________________________
> 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