Let me present to you my problem : I have a character vector x and I would like to obtain the indices of the elements of this vector that yielded exactly a match. For example, x=nom, pattern="b", I would to obtain 2 because "b" is on the second position. First program : nom <- c("a","b","ab") grep("b",nom) 2 3 Then I try the option extended =FALSE (instead of TRUE by default) and I obtain '2 3' a second time. Please can you help me : How can I obtain only 2 in using the grep function (without using the match function). Thanks you Julie AUBERT
Hi! For exact matches you can use == or is.element. To get the indices use which. e.g. => x<-c("a","b","ab") > x=="a"[1] TRUE FALSE FALSE> which((x=="a")==T)[1] 1 or e.g. is.element> is.element(x,"a")[1] TRUE FALSE FALSE> which(is.element(x,"a")==TRUE)[1] 1>Sincerely Eryk *********** REPLY SEPARATOR *********** On 7/8/2004 at 9:20 AM aubert at inapg.fr wrote:>>>Let me present to you my problem : >>> >>>I have a character vector x and I would like to obtain the indices of >>>the >>>elements of >>>this vector that yielded exactly a match. >>> >>>For example, x=nom, pattern="b", I would to obtain 2 because "b" is on >>>the >>>second position. >>> >>>First program : >>>nom <- c("a","b","ab") >>>grep("b",nom) >>>2 3 >>> >>>Then I try the option extended =FALSE (instead of TRUE by default) and I >>>obtain '2 3' a second time. >>> >>>Please can you help me : How can I obtain only 2 in using the grep >>>function >>>(without using the match function). >>> >>>Thanks you >>> >>>Julie AUBERT >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
grep("^b$",nom) will match "b" only. Alec Alec Stephenson Department of Statistics Macquarie University NSW 2109, Australia>>> "aubert at inapg.fr" <aubert at inapg.fr> 07/08/04 05:20pm >>>Let me present to you my problem : I have a character vector x and I would like to obtain the indices of the elements of this vector that yielded exactly a match. For example, x=nom, pattern="b", I would to obtain 2 because "b" is on the second position. First program : nom <- c("a","b","ab") grep("b",nom) 2 3 Then I try the option extended =FALSE (instead of TRUE by default) and I obtain '2 3' a second time. Please can you help me : How can I obtain only 2 in using the grep function (without using the match function). Thanks you Julie AUBERT ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi Christian, It works better now. Thanks a lot. Julie At 09:32 08/07/2004 +0200, you wrote:>Hi Julie, > >as I understand your question you only want indices for exact matches to >"b". This can be achieved using regular expressions (see ?regex) > >nom <- c("a","b","ab") >grep("^b$",nom) > >I hope this helps? > >Christian
Hi Julie, match is not exactly what you need, as it works with regular expressions and takes anything what includes a letter "b". For your case, there is perfectly suitable which(nom=="b") nom <- c("a","b","ab", "b") which(nom=="b") [1] 2 4 Jan - - - Original message: - - - From: r-help-bounces at stat.math.ethz.ch Send: 8.7.2004 9:24:58 To: r-help at stat.math.ethz.ch Subject: [R] Problem with the grep function Let me present to you my problem : I have a character vector x and I would like to obtain the indices of the elements of this vector that yielded exactly a match. For example, x=nom, pattern="b", I would to obtain 2 because "b" is on the second position. First program : nom <- c("a","b","ab") grep("b",nom) 2 3 Then I try the option extended =FALSE (instead of TRUE by default) and I obtain '2 3' a second time. Please can you help me : How can I obtain only 2 in using the grep function (without using the match function). Thanks you Julie AUBERT ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Or you must mark the word beginning with ^ and the end $ if you like to use grep. grep("^b$",nom) Sincerely Eryk *********** REPLY SEPARATOR *********** On 7/8/2004 at 9:20 AM aubert at inapg.fr wrote:>>>Let me present to you my problem : >>> >>>I have a character vector x and I would like to obtain the indices of >>>the >>>elements of >>>this vector that yielded exactly a match. >>> >>>For example, x=nom, pattern="b", I would to obtain 2 because "b" is on >>>the >>>second position. >>> >>>First program : >>>nom <- c("a","b","ab") >>>grep("b",nom) >>>2 3 >>> >>>Then I try the option extended =FALSE (instead of TRUE by default) and I >>>obtain '2 3' a second time. >>> >>>Please can you help me : How can I obtain only 2 in using the grep >>>function >>>(without using the match function). >>> >>>Thanks you >>> >>>Julie AUBERT >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
Hi You can use %in%> nom%in%"b"[1] FALSE TRUE FALSE which gives you a logical vector of exact matches> (1:3)[nom%in%"b"][1] 2 or charmatch> charmatch("b",nom)[1] 2> charmatch("ab",nom)[1] 3 if you expect only one exact match. But I expect someone can give you better answer. Cheers Petr On 8 Jul 2004 at 9:20, aubert at inapg.fr wrote:> Let me present to you my problem : > > I have a character vector x and I would like to obtain the indices of > the elements of this vector that yielded exactly a match. > > For example, x=nom, pattern="b", I would to obtain 2 because "b" is > on the second position. > > First program : > nom <- c("a","b","ab") > grep("b",nom) > 2 3 > > Then I try the option extended =FALSE (instead of TRUE by default) and > I obtain '2 3' a second time. > > Please can you help me : How can I obtain only 2 in using the grep > function (without using the match function). > > Thanks you > > Julie AUBERT > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
Hi, you can use regular expression with grep. For example:>exactmatch<-function(s,l){return(grep(paste('^',s,'$',sep=''),l))}>t<-c('a','ab','abc','c','ca','ab')> exactmatch('ab',t)[1] 2 6 HTH Marc -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Petr Pikal Sent: Thursday, July 08, 2004 2:28 PM To: aubert at inapg.fr; r-help at stat.math.ethz.ch Subject: Re: [R] Problem with the grep function Hi You can use %in%> nom%in%"b"[1] FALSE TRUE FALSE which gives you a logical vector of exact matches> (1:3)[nom%in%"b"][1] 2 or charmatch> charmatch("b",nom)[1] 2> charmatch("ab",nom)[1] 3 if you expect only one exact match. But I expect someone can give you better answer. Cheers Petr On 8 Jul 2004 at 9:20, aubert at inapg.fr wrote:> Let me present to you my problem : > > I have a character vector x and I would like to obtain the indices of > the elements of this vector that yielded exactly a match. > > For example, x=nom, pattern="b", I would to obtain 2 because "b" is > on the second position. > > First program : > nom <- c("a","b","ab") > grep("b",nom) > 2 3 > > Then I try the option extended =FALSE (instead of TRUE by default) and > I obtain '2 3' a second time. > > Please can you help me : How can I obtain only 2 in using the grep > function (without using the match function). > > Thanks you > > Julie AUBERT > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html