Hi, I am searching for the equivalent of the function Index from SAS. In SAS: index("abcd", "bcd") will return 2 because bcd is located in the 2nd cell of the abcd string. The equivalent in R should do this:> myIndex <- foo("abcd", "bcd") #return 2.What is the function that I am looking for? I want to use the return value in substr, like I do in SAS. thanks, y. baranan. [[alternative HTML version deleted]]
For simple searches, use grep with fixed=TRUE. Check ?grep. Nick Sabbe -- ping: nick.sabbe at ugent.be link: http://biomath.ugent.be wink: A1.056, Coupure Links 653, 9000 Gent ring: 09/264.59.36 -- Do Not Disapprove -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of yoav baranan Sent: maandag 25 oktober 2010 13:27 To: r-help at r-project.org Subject: [R] Find index of a string inside a string? Hi, I am searching for the equivalent of the function Index from SAS. In SAS: index("abcd", "bcd") will return 2 because bcd is located in the 2nd cell of the abcd string. The equivalent in R should do this:> myIndex <- foo("abcd", "bcd") #return 2.What is the function that I am looking for? I want to use the return value in substr, like I do in SAS. thanks, y. baranan. [[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.
I think what you want is 'regexpr':> regexpr("bcd", "aabcd")[1] 3 attr(,"match.length") [1] 3>On Mon, Oct 25, 2010 at 7:27 AM, yoav baranan <ybaranan at hotmail.com> wrote:> > Hi, > I am searching for the equivalent of the function Index from SAS. > > In SAS: index("abcd", "bcd") will return 2 because bcd is located in the 2nd cell of the abcd string. > The equivalent in R should do this: >> myIndex <- foo("abcd", "bcd") #return 2. > What is the function that I am looking for? > > I want to use the return value in substr, like I do in SAS. > > thanks, y. baranan. > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Maybe Matching Threads
- cross-correlation table with subscript or superscript to indicate significant differences
- get cells by the combination of their column and row names
- Assignment of values with different indexes
- Can PXElinux be used to dynamically pass DHCP option 252 to a PXE client?
- Unable to setup messaging listener