[R] grep

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Jan 24 18:23:43 CET 2002


On Thu, 24 Jan 2002, R. Heberto Ghezzo wrote:

> Hello, I have a problem with 'grep'
> I read several lines from a file
>  p <- readLines(pfile,n=8)
> now I have to check that a key word "JABBERWOCKY" is not in that segment
>
> a <- grep("JABBERWOCKY",p)
> which gives a 1 if the key is in the text or 'numeric(0)
> how can I test it
> if(a==1) gives Ok in one case and error:missing value where logical
> needed in the other.
> How can I coerce 'a' to be 0 or 1?
> thanks for any help.

You could test the length of the result, which is > 0 iff there was a match
in any of the lines.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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