Hi, I am a newbie to R and this may be too simple to ask. I am trying to find out a string function in R that returns the index of a character. For e.g. indexOf("Test1234", '4') would return 8. Is there a similar function in R. I tried searching the documentation and could find other useful string functions, but not the index function. Thanks in advance. Regards, Harsh Yadav [[alternative HTML version deleted]]
Hello, This does what you are looking for:> regexpr("4", "Test1234")[1] 8 attr(,"match.length") [1] 1 see ?regexpr also ?regexp for more details on regular expressions in R. HTH, Josh On Wed, Jun 30, 2010 at 7:48 PM, harsh yadav <harsh.delhi at gmail.com> wrote:> Hi, > > I am a newbie to R and this may be too simple to ask. > > I am trying to find out a string function in R that returns the index of a > character. > > For e.g. indexOf("Test1234", '4') would return 8. > > Is there a similar function in R. > I tried searching the documentation and could find other useful string > functions, but not the index function. > > Thanks in advance. > > Regards, > Harsh Yadav > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
harsh yadav wrote:> Hi, > > I am a newbie to R and this may be too simple to ask. > > I am trying to find out a string function in R that returns the index of a > character. > > For e.g. indexOf("Test1234", '4') would return 8.> regexpr("4", "Test1234") [1] 8 attr(,"match.length") [1] 1
On Jun 30, 2010, at 10:48 PM, harsh yadav wrote:> Hi, > > I am a newbie to R and this may be too simple to ask. > > I am trying to find out a string function in R that returns the > index of a > character. > > For e.g. indexOf("Test1234", '4') would return 8. > > Is there a similar function in R. > I tried searching the documentation and could find other useful string > functions, but not the index function.?grep ?strsplit > grep("4", strsplit("Test1234", "")[[1]]) [1] 8> > Thanks in advance. > > Regards, > Harsh Yadav > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
On Jun 30, 2010, at 10:48 PM, harsh yadav wrote:> Hi, > > I am a newbie to R and this may be too simple to ask. > > I am trying to find out a string function in R that returns the > index of a > character. > > For e.g. indexOf("Test1234", '4') would return 8.?grep ?strsplit > grep("4", strsplit("Test1234", "")[[1]]) [1] 8> > Is there a similar function in R. > I tried searching the documentation and could find other useful string > functions, but not the index function. > > Thanks in advance. > > Regards, > Harsh Yadav