[R] if(grep())

Michael Lapsley mlapsley at ndirect.co.uk
Tue Oct 12 01:16:40 CEST 1999


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

My solution:

> trygrep <-function(x,y)
+ if(!is.na(grep(x,y)[1]))
+ print("yes") else
+ print("NO")
>
> trygrep("foo","bafoobar")
[1] "yes"
> trygrep("foo","bar")
[1] "NO"
> 

Regards,

Michael

This is not exactly intuitive and it took me ages to get past what should be a
very simple couple of lines.

Am I missing something more obvious?  I'm sure that if I need to do this
in 6 months time I'm not going to remember how I did it!

----------------------------------
E-Mail: Michael Lapsley <mlapsley at ndirect.co.uk>
Date: 12-Oct-99
Time: 00:04:03

This message was sent by XFMail
----------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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