Dear R-users, is there a way to pass a list of patterns to the grep function? I vaguely remember something with %in% operator... Thanks, St?phane. -- "La science a certes quelques magnifiques r?ussites ? son actif mais ? tout prendre, je pr?f?re de loin ?tre heureux plut?t qu'avoir raison." D. Adams -- AGC website <http://www.genoscope.cns.fr/agc> St?phane CRUVEILLER Ph. D. Genoscope - Centre National de S?quencage Atelier de G?nomique Comparative 2, Rue Gaston Cremieux CP 5706 91057 Evry Cedex - France Phone: +33 (0)1 60 87 84 58 Fax: +33 (0)1 60 87 25 14 scruveil at genoscope.cns.fr
Try this:> grep("b|c|d", letters, value = TRUE)[1] "b" "c" "d" On 10/16/06, St?phane CRUVEILLER <scruveil at genoscope.cns.fr> wrote:> Dear R-users, > > is there a way to pass a list of patterns to the grep function? I > vaguely remember something with %in% operator... > > > Thanks, > > > St?phane. > > > > -- > "La science a certes quelques magnifiques r?ussites ? son actif mais > ? tout prendre, je pr?f?re de loin ?tre heureux plut?t qu'avoir raison." > D. Adams > -- > AGC website <http://www.genoscope.cns.fr/agc> > St?phane CRUVEILLER Ph. D. > Genoscope - Centre National de S?quencage > Atelier de G?nomique Comparative > 2, Rue Gaston Cremieux CP 5706 > 91057 Evry Cedex - France > Phone: +33 (0)1 60 87 84 58 > Fax: +33 (0)1 60 87 25 14 > scruveil at genoscope.cns.fr > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Ooops sorry for html tags... Just forgot to edit the message before sending it... So back to my question: Thx for the hint, but what would I have used if "b","c" and "d" were values of a dataframe for instance? X is for instance a dataframe: >X Mypatterns 1 pattern1 2 pattern2 3 pattern3 Y is another dataframe. If I do: grep(X$Mypatterns,Y) this will take into account only the first pattern... I could use a loop but I vaguely remember an elegant trick that combined grep and %in%. St?phane.
> DF <- data.frame(pat = letters[1:3]) > grep(paste(DF$pat, collapse = "|"), letters, value = TRUE)[1] "a" "b" "c" On 10/16/06, St?phane CRUVEILLER <scruveil at genoscope.cns.fr> wrote:> Ooops sorry for html tags... Just forgot to edit the message > before sending it... > So back to my question: > > Thx for the hint, but what would I have used if "b","c" and "d" > were values of a dataframe for instance? > > X is for instance a dataframe: > >X > Mypatterns > 1 pattern1 > 2 pattern2 > 3 pattern3 > > Y is another dataframe. > > If I do: > > grep(X$Mypatterns,Y) this will take into account only the first pattern... > I could use a loop but I vaguely remember an elegant trick that combined > grep and %in%. > > > St?phane. > >
Hi Stephane, St?phane CRUVEILLER <scruveil <at> genoscope.cns.fr> writes:> is there a way to pass a list of patterns to the grep function? I > vaguely remember something with %in% operator...I think you are looking for the %in% and %nin% which are part of Design package, and also in Hmisc library. You have to install and load these packages to access these functions. Anupam.