[R] Any simple way to subset a vector of strings that do contain a particular substring ?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jun 19 16:09:11 CEST 2008


regexpr("ba", strings)

On Thu, Jun 19, 2008 at 1:18 AM, Daren Tan <daren76 at hotmail.com> 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.



More information about the R-help mailing list