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. Thanks in advance, David S. David White sdavidwhite at bigfoot.com Columbus, Ohio -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David White <dwhite at ling.ohio-state.edu> writes:> 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.grep(), I believe -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear David, At 12:23 PM 24/08/2001 -0400, 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.The function grep or regexpr should do what you want; for example: > vec <- c('tsibi', 'tSibi', 'tibi', 'kibi') > grep('[sSk]', vec) [1] 1 2 4 > regexpr('[sSk]', vec) [1] 2 2 -1 1 attr(,"match.length") [1] 1 1 -1 1 > See the help files for details. I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._