[R] grep for multiple pattern?

Rainer M Krug Rainer at krugs.de
Thu Feb 13 17:25:02 CET 2014


On 02/13/14, 17:23 , jim holtman wrote:
> use the "|" in regular expressions:
> 
>  grep(c("an|em|eb", month.name <http://month.name/>) 

Thanks - again a reason to learn regexp.

Cheers,

Rainer

> 
> 
> Jim Holtman
> Data Munger Guru
>  
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
> 
> 
> On Thu, Feb 13, 2014 at 9:43 AM, Rainer M Krug <Rainer at krugs.de
> <mailto:Rainer at krugs.de>> wrote:
> 
>     Hi
> 
>     I want to search for multiple pattern as grep is doing for a single
>     pattern, but this obviously not work:
> 
>     > grep("an", month.name <http://month.name>)
>     [1] 1
>     > grep("em", month.name <http://month.name>)
>     [1]  9 11 12
>     > grep("eb", month.name <http://month.name>)
>     [1] 2
>     > grep(c("an", "em", "eb"), month.name <http://month.name>)
>     [1] 1
>     Warning message:
>     In grep(c("an", "em", "eb"), month.name <http://month.name>) :
>       argument 'pattern' has length > 1 and only the first element will
>     be used
>     >
> 
>     Is there an equivalent which returns the positions as grep is doing, but
>     not using the strict full-string matching of match()?
> 
>     I could obviously do:
> 
>     > unlist( sapply(pat, grep, month.name <http://month.name> ) )
>      an em1 em2 em3  eb
>       1   9  11  12   2
> 
>     but is there a more compact command I am missing?
> 
>     Thanks,
> 
>     Rainer
> 
>     --
>     Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
>     Biology, UCT), Dipl. Phys. (Germany)
> 
>     Centre of Excellence for Invasion Biology
>     Stellenbosch University
>     South Africa
> 
>     Tel :       +33 - (0)9 53 10 27 44
>     <tel:%2B33%20-%20%280%299%2053%2010%2027%2044>
>     Cell:       +33 - (0)6 85 62 59 98
>     <tel:%2B33%20-%20%280%296%2085%2062%2059%2098>
>     Fax :       +33 - (0)9 58 10 27 44
>     <tel:%2B33%20-%20%280%299%2058%2010%2027%2044>
> 
>     Fax (D):    +49 - (0)3 21 21 25 22 44
>     <tel:%2B49%20-%20%280%293%2021%2021%2025%2022%2044>
> 
>     email:      Rainer at krugs.de <mailto:Rainer at krugs.de>
> 
>     Skype:      RMkrug
> 
> 
>     ______________________________________________
>     R-help at r-project.org <mailto: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.
> 
> 

-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 560 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140213/b17891bc/attachment-0002.bin>


More information about the R-help mailing list