[R] logical and NA

Tony Plate tplate at blackmesacapital.com
Wed Sep 4 18:11:22 CEST 2002


I sometimes use identical(T, boolean.expression) for this purpose, e.g.:

 > identical(T,3*4==12)
[1] TRUE
 > identical(T,3*4==11)
[1] FALSE
 > identical(T,3*NA==11)
[1] FALSE

However, this is not ideal because if your objects have names, identical 
will return false if the names don't match:

 > 3*4==c(a=12)
    a
TRUE
 > identical(T,3*4==c(a=12))
[1] FALSE
 > identical(c(a=TRUE),3*4==c(a=12))
[1] TRUE

You can get around this problem by wrapping the expression with as.logical 
(to strip names), but it's starting to get cumbersome (though the 
expression still only occurs once):

 > identical(TRUE,as.logical(3*4==c(a=12)))
[1] TRUE
 >

-- Tony Plate

At 10:29 AM 9/4/2002 +0200, Christian Hoffmann wrote:
>Hi there, (sorry Martin, ich habe den falschen Empfaenger erwischt :-(  )
>
>Is there a function in the base package or elsewhere which returns TRUE 
>and not NA, even if NA is involved: (I did not find in the help system :-(  )
>
>if ( x==something & !is.na(something))
>
>or
>
>if ( x==something & !is.na(x==something))
>
>If "something" is complicated, a function would be handy.
>
>Thanks
>--christian
>
>
>Dr.sc.math.Christian W. Hoffmann
>Mathematics and Statistical Computing
>Landscape Dynamics and Spatial Development
>Swiss Federal Research Institute WSL
>Zuercherstrasse 111
>CH-8903 Birmensdorf, Switzerland
>phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
>e-mail: christian.hoffmann at wsl.ch
>www: http://www.wsl.ch/staff/christian.hoffmann/
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list