# [R] or of a logical vector

Thu Aug 5 21:15:22 CEST 2004

```I don't know how careful about coercing type you need to be.

Something like TRUE %in% outer(v,v,"|") may work for you but simpler
functions that do arithmetic and coerce the answer back to logical [e.g.,
as.logical(max(v))] might suffice.

> xt <- c(T,T,T)
> xf <- c(T,F,F)
> outer(xt, xf, "|")
[,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE
> outer(xf, xf, "|")
[,1]  [,2]  [,3]
[1,] TRUE  TRUE  TRUE
[2,] TRUE FALSE FALSE
[3,] TRUE FALSE FALSE
> TRUE %in% outer(xf, xf, "|")
 TRUE
> TRUE %in% outer(!xt, !xt, "|")
 FALSE
> #if you don't mind sloppiness
> as.logical(max(xf))
 TRUE
> as.logical(max(!xt))
 FALSE
> as.logical(sum(xf^2))
 TRUE
> as.logical(sum((!xt)^2))
 FALSE
> #be careful
> as.logical(max(c(-1,0,0))
+ )
 FALSE
>

bob

-----Original Message-----
From: Ben Wittner [mailto:bwittner at jimmy.harvard.edu]
Sent: Thursday, August 05, 2004 12:38 PM
To: r-help at stat.math.ethz.ch
Subject: [R] or of a logical vector

Is there some fast (built-in?) way to get the OR of all the elements in a
logical vector?

In other words, is there some fast (built-in) version of the function vor
below?

Thanks.

-Ben

vor <- function(v) {
ans <- v
if (length(v) > 1)
for (i in 2:length(v))
ans <- ans | v[i]
ans
}

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help