lindeman at bard.edu
2007-Jan-20 18:30 UTC
[R] simple q: returning a logical vector of substring matches
I'm a relative R novice, and sometimes the simple things trip me up.
Suppose I have
a <- c("apple", "pear")
and I want a logical vector of whether each of these strings contains
"ear" (in this case, F T). What is the idiom?
Quizzically,
Mark Lindeman
Marc Schwartz
2007-Jan-20 18:58 UTC
[R] simple q: returning a logical vector of substring matches
On Sat, 2007-01-20 at 13:30 -0500, lindeman at bard.edu wrote:> I'm a relative R novice, and sometimes the simple things trip me up. > > Suppose I have > > a <- c("apple", "pear") > > and I want a logical vector of whether each of these strings contains > "ear" (in this case, F T). What is the idiom? > > Quizzically, > Mark LindemanSee ?grep and ?regexp a <- c("apple", "pear")> grep("ear", a)[1] 2> grep("ear", a, value = TRUE)[1] "pear" If you actually want the answer to be FALSE TRUE, then:> a %in% grep("ear", a, value = TRUE)[1] FALSE TRUE In that case see ?"%in%" HTH, Marc Schwartz
Christos Hatzis
2007-Jan-20 18:58 UTC
[R] simple q: returning a logical vector of substring matches
You can try the following:
a == grep("ear", a, value=T)
-Christos
Christos Hatzis, Ph.D.
Nuvera Biosciences, Inc.
400 West Cummings Park
Suite 5350
Woburn, MA 01801
Tel: 781-938-3830
www.nuverabio.com
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of lindeman at bard.edu
Sent: Saturday, January 20, 2007 1:31 PM
To: r-help at stat.math.ethz.ch
Subject: [R] simple q: returning a logical vector of substring matches
I'm a relative R novice, and sometimes the simple things trip me up.
Suppose I have
a <- c("apple", "pear")
and I want a logical vector of whether each of these strings contains
"ear"
(in this case, F T). What is the idiom?
Quizzically,
Mark Lindeman
______________________________________________
R-help at stat.math.ethz.ch 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.
jim holtman
2007-Jan-20 18:59 UTC
[R] simple q: returning a logical vector of substring matches
try 'regexpr'> a <- c("apple", "pear") > regexpr('ear',a)!=-1[1] FALSE TRUE>On 1/20/07, lindeman@bard.edu <lindeman@bard.edu> wrote:> > I'm a relative R novice, and sometimes the simple things trip me up. > > Suppose I have > > a <- c("apple", "pear") > > and I want a logical vector of whether each of these strings contains > "ear" (in this case, F T). What is the idiom? > > Quizzically, > Mark Lindeman > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Gabor Grothendieck
2007-Jan-20 19:32 UTC
[R] simple q: returning a logical vector of substring matches
Using the builtin month.abb try this:
regexpr("ov", month.abb) > 0
Although not needed here, if "ov" were a character string that could
have
special characters such as . and * that have special meaning in a regular
expression then do this to prevent such interpretation:
regexpr("ov", month.abb, fixed = TRUE) > 0
See ?regexpr
On 1/20/07, lindeman at bard.edu <lindeman at bard.edu>
wrote:> I'm a relative R novice, and sometimes the simple things trip me up.
>
> Suppose I have
>
> a <- c("apple", "pear")
>
> and I want a logical vector of whether each of these strings contains
> "ear" (in this case, F T). What is the idiom?
>
> Quizzically,
> Mark Lindeman
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>