[R] if(grep())

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Oct 12 09:34:53 CEST 1999


Thomas Lumley <thomas at biostat.washington.edu> writes:

> On Tue, 12 Oct 1999, Michael Lapsley wrote:
> 
> > Would anyone care to comment if this is the best solution to this problem?
> > 
> > > trygrep <-function(x,y)
> > + if(grep(x,y))
> > + print("yes") else
> > + print("NO")
> > >  
> > >trygrep("foo","bafoobar")
> > [1] "yes"
> > >
> > > trygrep("foo","bar")
> > Error in if (grep(x, y)) print("yes") else print("NO") : missing value where
> > logical needed
> 
> 
> You could try 
> 	if(length(grep(x,y))==0)

Or, with some abuse of semantics but kind of nice looking:

	any(grep(x,y))

since any is FALSE on an empty vector, but TRUE on any vector of
positive integers.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list