Daren Tan
2008-Jun-19 05:18 UTC
[R] Any simple way to subset a vector of strings that do contain a particular substring ?
For example, strings <- c("aaaa", "bbbb","ccba"). How to get "aaaa", "bbbb" that do not contain "ba" ? _________________________________________________________________ [[alternative HTML version deleted]]
Moshe Olshansky
2008-Jun-19 05:48 UTC
[R] Any simple way to subset a vector of strings that do contain a particular substring ?
strings[-grep("ba",strings)] --- On Thu, 19/6/08, Daren Tan <daren76 at hotmail.com> wrote:> From: Daren Tan <daren76 at hotmail.com> > Subject: [R] Any simple way to subset a vector of strings that do contain a particular substring ? > To: r-help at stat.math.ethz.ch > Received: Thursday, 19 June, 2008, 3:18 PM > For example, > > strings <- c("aaaa", > "bbbb","ccba"). > > How to get "aaaa", "bbbb" that do not > contain "ba" ? > _________________________________________________________________ > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Bernhard
2008-Jun-19 05:56 UTC
[R] Any simple way to subset a vector of strings that do contain a particular substring ?
Hi Daren. you could try something like: strings[setdiff(1:length(strings), grep("ba", strings))] ?grep as well as ?regexpr will help as well! Bernhard 2008/6/19 Daren Tan <daren76 at hotmail.com>:> > > For example, > > strings <- c("aaaa", "bbbb","ccba"). > > How to get "aaaa", "bbbb" that do not contain "ba" ?
Philipp Pagel
2008-Jun-19 06:19 UTC
[R] Any simple way to subset a vector of strings that do contain a particular substring ?
> strings <- c("aaaa", "bbbb","ccba"). > > How to get "aaaa", "bbbb" that do not contain "ba" ?I think this is what you want:> foo <- c("aaaa", "bbbb","ccba") > foo[-grep('ba', foo)][1] "aaaa" "bbbb" cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel
Gabor Grothendieck
2008-Jun-19 14:09 UTC
[R] Any simple way to subset a vector of strings that do contain a particular substring ?
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.