[Rd] unary "-" doesn't convert logicals (PR#1127)

john_heumann@agilent.com john_heumann@agilent.com
Fri, 12 Oct 2001 00:12:52 +0200 (MET DST)


Full_Name: John M. Heumann
Version: 1.3.0
OS: Windows NT4/SP5
Submission from: (NULL) (192.25.140.108)


If unary "-" is applied to a logical vector, the resulting value
is computed correctly (i.e. coerced to a numeric), but the type
(incorrectly) remains logical: 

> x <- 1:10
> p1 <- (x > 5)
> p2 <- (-p1)
> is.logical(p2)
[1] TRUE
> is.numeric(p2)
[1] FALSE
> as.numeric(p1)
 [1] 0 0 0 0 0 1 1 1 1 1
> as.numeric(p2)
 [1]  0  0  0  0  0 -1 -1 -1 -1 -1

Since "==" always does binary comparison, this can lead to
some bizarre behavior:

> p1
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> p2
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> (p1 == p2)
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
((!p1) == (!p2))
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._