Benjamin Dickgiesser wrote: > > I am looking for a way to compare if every element of a vector is > 0. > > i.e. > while(abs(U) > 0) > { > > .. > } > Notice that abs(U) [and, in general, most functions that are defined on scalars, like sin, cos, exp, ...], when U is a vector, operates on the _elements_ of U, so abs(U) is just a vector of non-negative elements.
Likewise, (U > 0) [and, in general, most relations that are defined on scalars, like (U != 0), (U == 0), (U >= 1 & U <= 2)], when U is a vector, operates on the _elements_ of U, so (U > 0) is just a vector of logical values. So, you must take some operation that will check if all components of a vector of logical (boolean) elements are non-zero. The most obvious solution is to _multiply_ those logical elements, because FALSE will become zero, and zero * anything = zero, so if any component of U is <= 0, you get zero: 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) Sanity check: U <- c(-1, 1, 2) all(U > 0) # FALSE U[1] <- 3 all(U > 0) # TRUE Alberto Monteiro ______________________________________________ R-help@stat.math.ethz.ch 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.