[R] is there a function like %in% for characters?

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Sun Apr 3 20:07:21 CEST 2005



Terry Mu wrote on 4/2/2005 9:38 PM:
> like:
> 
> "a" %in% "abcd"
> TRUE
> 
> Thanks.
> 


See ?regexpr.

regexpr("a", "abcd") > 0

However, the first argument is not vectorized so you may also need 
something like:

 > sapply(c("a", "b", "e"), regexpr, c("abcd", "bcde")) > 0
          a    b     e
[1,]  TRUE TRUE FALSE
[2,] FALSE TRUE  TRUE

Be sure to read up on regular expressions if pursuing this option.

HTH,

--sundar




More information about the R-help mailing list