[R] return value for grep
David Winsemius
dwinsemius at comcast.net
Sun Oct 31 04:13:42 CET 2010
On Oct 30, 2010, at 10:51 PM, Ulrich wrote:
> Hi,
>
> is it possible to easily change the return value for the grep
> function for cases where there is no match, for example the value 0
> or "No" instead of integer (0) )?
>
Seems like is should be pretty easy. Test for length(grep(...)) == 0
or equivalently !length(grep(...))
> gvec <- function(patt, x) sapply (x, function(y) if ( !
length(grep(patt, y)) ) {"No"} else {grep(patt, y)} )
> gvec("b", c("abc", "abc\n"))
abc abc\n
1 1
> gvec("\n", c("abc", "abc\n"))
abc abc\n
"No" "1"
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list