[R] abs(U) > 0 where U is a vector?

Charilaos Skiadas skiadas at hanover.edu
Wed Mar 14 12:45:37 CET 2007


On Mar 14, 2007, at 7:05 AM, Alberto Monteiro wrote:
> prod(U > 0)
>
> But this is not the most "elegant" solution, because there is
> a function to check if all [and another to check if any] component
> of a vector of booleans are [is] true: it's all(V) [resp. any(V)].
> So:
> all(U > 0)

Just for the record, there is a actually a slight difference in the  
two calls of prod and all, which may or may not be important in the  
OP's case, in how they deal with NA's:

 > x<-c(-3,NA,2)
 > all(x>0)
[1] FALSE
 > prod(x>0)
[1] NA
 > x<-c(3,NA,2)
 > all(x>0)
[1] NA
 > prod(x>0)
[1] NA

These are of course all as expected, just something to keep in mind.

And in any case, "all" is as Alberto says more elegant, and  
semantically much more clear. (And not that it matters, but it is  
also somewhat faster).

> Alberto Monteiro

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list