Hi,
I am looking for a way to compare if every element of a vector is > 0.
i.e.
while(abs(U) > 0)
{
..
}
is there a function for this or do I have to write one?
I'd appreciate your help!
Benjamin
try: all(U > 0)
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Benjamin Dickgiesser" <dickgiesser at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, March 14, 2007 11:35 AM
Subject: [R] abs(U) > 0 where U is a vector?
> Hi,
>
> I am looking for a way to compare if every element of a vector is >
> 0.
>
> i.e.
> while(abs(U) > 0)
> {
>
> ..
> }
>
> is there a function for this or do I have to write one?
>
> I'd appreciate your help!
>
> Benjamin
>
> ______________________________________________
> R-help at 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Benjamin Dickgiesser wrote:> Hi, > > I am looking for a way to compare if every element of a vector is > 0. > > i.e. > while(abs(U) > 0) > { > > .. > } > > is there a function for this or do I have to write one? > > I'd appreciate your help! > > Benjamin >Hi, If I understand you correctly, you are not far, is this what you want : while(all(abs(U) > 0)){ ... } Cheers, Romain -- Mango Solutions data analysis that delivers Tel: +44(0) 1249 467 467 Fax: +44(0) 1249 467 468 Mob: +44(0) 7813 526 123
Thx for all your responses!
while(all(abs(U) > 0))
{
..
}
was what I am looking for.
On 3/14/07, Benjamin Dickgiesser <dickgiesser at gmail.com>
wrote:> Hi,
>
> I am looking for a way to compare if every element of a vector is > 0.
>
> i.e.
> while(abs(U) > 0)
> {
>
> ..
> }
>
> is there a function for this or do I have to write one?
>
> I'd appreciate your help!
>
> Benjamin
>
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