venkata kirankumar
2009-Feb-19 06:15 UTC
[R] problem with using %in% condition while using in if() condition
Hi all, I got one problem with using %in% condition while using in if() condition where I used the condition as if(SubFinSpt$SPECIMENTYP %in% CAP$SPECIMENTYP) this "if()" condition is in "else" condition and hear "*SubFinSpt$SPECIMENTYP*" having only one value but "*CAP$SPECIMENTYP" *having nearly 20 SPECIMENTYP's while applying this condition I got one warning that says only first element is checked and after that warning it is executing normally and giving results but i want to know why it is giving this warning can any one explain why it is comming and how to resolve it thanks in advance [[alternative HTML version deleted]]
Petr PIKAL
2009-Feb-19 07:02 UTC
[R] Odp: problem with using %in% condition while using in if() condition
Hi r-help-bounces at r-project.org napsal dne 19.02.2009 07:15:00:> Hi all, > I got one problem with using %in% condition while using in if()condition> where > I used the condition as > > > if(SubFinSpt$SPECIMENTYP %in% CAP$SPECIMENTYP)SubFinSpt$SPECIMENTYP %in% CAP$SPECIMENTYP gives you logical vector "if" works only with the first value of this vector see ?"if" ?"ifelse" Regards Petr> > this "if()" condition is in "else" condition > and hear "*SubFinSpt$SPECIMENTYP*" having only one value but > "*CAP$SPECIMENTYP" > *having nearly 20 SPECIMENTYP's > > while applying this condition I got one warning that says only first > element is checked > and after that warning it is executing normally and giving results > but i want to know why it is giving this warning > > can any one explain why it is comming and how to resolve it > > thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Greg Snow
2009-Feb-19 17:37 UTC
[R] problem with using %in% condition while using in if() condition
The %in% operator returns a vector of logicals the same length as the vector to the left. The if program flow operator expects a single logical value, not a vector, since you are giving it a vector it looks at just the 1st element, ignores the rest and gives the warning. This warning should be taken seriously since it indicates that what is happening and what you intend probably do not match. If you tell us more about what you are trying to do, we can give more help. Some possibilities: use any() or all() to reduce the vector to a single logical value. Use ifelse(), subscripting, or some other method to accomplish what you want. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of venkata kirankumar > Sent: Wednesday, February 18, 2009 11:15 PM > To: r-help at r-project.org > Subject: [R] problem with using %in% condition while using in if() > condition > > Hi all, > I got one problem with using %in% condition while using in if() > condition > where > I used the condition as > > > if(SubFinSpt$SPECIMENTYP %in% CAP$SPECIMENTYP) > > this "if()" condition is in "else" condition > and hear "*SubFinSpt$SPECIMENTYP*" having only one value but > "*CAP$SPECIMENTYP" > *having nearly 20 SPECIMENTYP's > > while applying this condition I got one warning that says only first > element is checked > and after that warning it is executing normally and giving results > but i want to know why it is giving this warning > > can any one explain why it is comming and how to resolve it > > thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.