regexpr("ba", strings)

On Thu, Jun 19, 2008 at 1:18 AM, Daren Tan <[EMAIL PROTECTED]> wrote:
>
>
> For example,
>
> strings <- c("aaaa", "bbbb","ccba").
>
> How to get "aaaa", "bbbb" that do not contain "ba" ?

Here are three ways:

> strings[regexpr("ba", strings) < 0]
[1] "aaaa" "bbbb"

> setdiff(strings, grep("ba", strings, value = TRUE))
[1] "aaaa" "bbbb"

> strings[-grep("ba", c(strings, "ba"))]
[1] "aaaa" "bbbb"

Note that in the last case we had to ensure that there is at least
one "ba" string by appending one since it would otherwise fail
in the case that there were no "ba" strings.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to