if(grep(...) == 1) was [R] (no subject)
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Fri Jan 17 09:07:03 CET 2003
On Thu, 16 Jan 2003, graham lawrence wrote:
> Why does the second grep reject when the first grep accepts?
> Why does regexpr accept what grep rejects?
It's the if() that is different. grep returns an integer vector of
matching indices, and numeric(0) == 1 is logical(0), which gives nothing
to test (and I guess the error message could be improved),
The idiom often is if(length(grep(...))).
regexpr returns something for each match, but I suspect you want to test
> 0, not ==1. See the help page.
> >eqtn
> [1] "(1.2*A%*%B)/2"
> >if(grep("A\\%\\*\\%B",eqtn)==1)erind<-1
> >if(grep("A\\%\\*\\%A",eqtn)==1)erind<-1
> Error in if (grep("A\\%\\*\\%A", eqtn) == 1) erind <- 1 :
> missing value where logical needed
> >if(regexpr("A\\%\\*\\%A",eqtn)==1)erind<-1
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list