[R] simple q: returning a logical vector of substring matches
marc_schwartz at comcast.net
Sat Jan 20 19:58:04 CET 2007
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?
> Mark Lindeman
See ?grep and ?regexp
a <- c("apple", "pear")
> grep("ear", a)
> grep("ear", a, value = TRUE)
If you actually want the answer to be FALSE TRUE, then:
> a %in% grep("ear", a, value = TRUE)
 FALSE TRUE
In that case see ?"%in%"
More information about the R-help