[R] matching character substrings

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 24 18:30:09 CEST 2001


On Fri, 24 Aug 2001, David White wrote:

>
> Hello,
>
> Is there a function for searching a string for a given character? For
> example, I have
> transcriptions like
>
> tsibi
> tSibi
> tibi
> kibi
>
> I want to search the character object for the presence of 's' or 'S' or
> 'k'.
>
> I thought perhaps pmatch(c("k", "S", "s"), x) would work for me, but it
> wants to compare from the start of the string and returns NA for "S" and
> "s" because they are not at the head of the string.

regexpr or grep, depending on the level of info you want.

regexpr("[sSk]", c("tsibi", "tSibi", "tibi", "kibi"))
[1]  2  2 -1  1
attr(,"match.length")
[1]  1  1 -1  1
grep("[sSk]", c("tsibi", "tSibi", "tibi", "kibi"))
[1] 1 2 4


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list