OK, this should be trivial but I'm not finding it. I want to compress the test, if (i==7 | i==10 | i==30 | i==50) {} into something like if (i in c(7,10,30,50)) {} so I can build a "excludes" vector excludes <- c(7,10,30,50) and test if (i in excludes) {} However, I'm not finding a clue on how to accomplish this, if it can be done. Would someone with more R experience lend a helping hand please? A reference (so I can continue learning) would also be appreciated. Thanks... -=d David Thompson, Ph.D., P.E., D.WRE, CFM Civil Engineer/Hydrologist
> OK, this should be trivial but I'm not finding it. I want to compress > the test, > > if (i==7 | i==10 | i==30 | i==50) {} > > into something like > > if (i in c(7,10,30,50)) {} > > so I can build a "excludes" vector > > excludes <- c(7,10,30,50) > > and test > > if (i in excludes) {}Works for me. excludes <- c(7,10,30,50) for(i in excludes) { print(i)} for(i in 5:30) { if( i %in% excludes) {print(i)} } What error messages are you getting? -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
Take a look at ?any. On Thu, Dec 11, 2008 at 3:11 PM, David B. Thompson, Ph.D., P.E., D.WRE, CFM <drdbthompson at gmail.com> wrote:> OK, this should be trivial but I'm not finding it. I want to compress the > test, > > if (i==7 | i==10 | i==30 | i==50) {} > > into something like > > if (i in c(7,10,30,50)) {} > > so I can build a "excludes" vector > > excludes <- c(7,10,30,50) > > and test > > if (i in excludes) {} > > However, I'm not finding a clue on how to accomplish this, if it can be > done. Would someone with more R experience lend a helping hand please? A > reference (so I can continue learning) would also be appreciated. > > Thanks... > > -=d > > David Thompson, Ph.D., P.E., D.WRE, CFM > Civil Engineer/Hydrologist > > ______________________________________________ > 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. >
i %in% c(7,10,30,50) On Thu, Dec 11, 2008 at 12:11 PM, David B. Thompson, Ph.D., P.E., D.WRE, CFM <drdbthompson@gmail.com> wrote:> OK, this should be trivial but I'm not finding it. I want to compress the > test, > > if (i==7 | i==10 | i==30 | i==50) {} > > into something like > > if (i in c(7,10,30,50)) {} > > so I can build a "excludes" vector > > excludes <- c(7,10,30,50) > > and test > > if (i in excludes) {} > > However, I'm not finding a clue on how to accomplish this, if it can be > done. Would someone with more R experience lend a helping hand please? A > reference (so I can continue learning) would also be appreciated. > > Thanks... > > -=d > > David Thompson, Ph.D., P.E., D.WRE, CFM > Civil Engineer/Hydrologist > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]