[R] grep function with patterns list...

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 16 15:56:46 CEST 2006


> 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.
>
>



More information about the R-help mailing list