Why the last "which" in the following example doesn't work? Is there a simple way to identify the indices of array elements that meet multiple criteria? Thanks. YC Tao> x<-1:10 > which(x<5)[1] 1 2 3 4> which(x>2)[1] 3 4 5 6 7 8 9 10> which(x<5 && x>2)numeric(0) __________________________________ Dress up your holiday email, Hollywood style. Learn more.
Because you have one too many `&'. Andy> From: Y. C. Tao > > Why the last "which" in the following example doesn't > work? Is there a simple way to identify the indices of > array elements that meet multiple criteria? > > Thanks. > > YC Tao > > > x<-1:10 > > which(x<5) > [1] 1 2 3 4 > > which(x>2) > [1] 3 4 5 6 7 8 9 10 > > which(x<5 && x>2) > numeric(0) > > __________________________________ > > Dress up your holiday email, Hollywood style. Learn more. > > ______________________________________________ > 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 > >
> which(x < 5 & x > 2)[1] 3 4 From the help page for Logical Operators: ?"!" '&' and '&&' indicate logical AND and '|' and '||' indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in 'if' clauses. Y. C. Tao wrote:> Why the last "which" in the following example doesn't > work? Is there a simple way to identify the indices of > array elements that meet multiple criteria? > > Thanks. > > YC Tao > > >>x<-1:10 >>which(x<5) > > [1] 1 2 3 4 > >>which(x>2) > > [1] 3 4 5 6 7 8 9 10 > >>which(x<5 && x>2) > > numeric(0)-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894