Jagarlamudi, Choudary
2005-May-20 17:58 UTC
[R] comparing a vactor of values in IF statement.
Hi, my vector V<- c(1,0.5,0.06,0.056,0.01,0.04,0.4,0.9,0.82,0.1) if( V > 0.5) { V <- 1 - V } I get a warning saying only the first element will be used in comparing (if V > 0.5). However, my results tell me vis-versa ,it actually compares every element of the vector V with 0.5 and that is waht i want it to do. Using a for loop is expensive and time consuming since my actual vector is 15000 values. Any help to rectify the warning i get is appreciated. Thank You. Choudary Jagarlamudi Instructor Southwestern Oklahoma State University STF 254 100 campus Drive Weatherford OK 73096 Tel 580-774-7136 [[alternative HTML version deleted]]
Gabor Grothendieck
2005-May-20 18:07 UTC
[R] comparing a vactor of values in IF statement.
Try V <- ifelse(V > 0.5, 1-V, V) or V <- pmin(V, 1-V) On 5/20/05, Jagarlamudi, Choudary <choudary.jagar at swosu.edu> wrote:> Hi, > > my vector V<- c(1,0.5,0.06,0.056,0.01,0.04,0.4,0.9,0.82,0.1) > > if( V > 0.5) { V <- 1 - V } > > I get a warning saying only the first element will be used in comparing (if V > 0.5). > However, my results tell me vis-versa ,it actually compares every element of the vector V with 0.5 and that is waht i want it to do. > Using a for loop is expensive and time consuming since my actual vector is 15000 values. > > Any help to rectify the warning i get is appreciated. > > Thank You. > > Choudary Jagarlamudi > Instructor > Southwestern Oklahoma State University > STF 254 > 100 campus Drive > Weatherford OK 73096 > Tel 580-774-7136 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >
Both V[V > 0.5] <- 1 - V[V > 0.5] and ifelse(V>0.5, 1-V,V) should do it. Ravi. -------------------------------------------------------------------------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu --------------------------------------------------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Jagarlamudi, Choudary > Sent: Friday, May 20, 2005 1:59 PM > To: r-help at stat.math.ethz.ch > Subject: [R] comparing a vactor of values in IF statement. > > Hi, > > my vector V<- c(1,0.5,0.06,0.056,0.01,0.04,0.4,0.9,0.82,0.1) > > if( V > 0.5) { V <- 1 - V } > > I get a warning saying only the first element will be used in comparing > (if V > 0.5). > However, my results tell me vis-versa ,it actually compares every element > of the vector V with 0.5 and that is waht i want it to do. > Using a for loop is expensive and time consuming since my actual vector is > 15000 values. > > Any help to rectify the warning i get is appreciated. > > Thank You. > > Choudary Jagarlamudi > Instructor > Southwestern Oklahoma State University > STF 254 > 100 campus Drive > Weatherford OK 73096 > Tel 580-774-7136 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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