[R] how to pass multiple pattern varibles to grep()

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Sep 23 18:34:38 CEST 2016


The | symbol has a completely different meaning in R syntax than it has in 
regular expression syntax. R hands of pattern strings to the regex library 
without looking inside the strings any more than it has to. Likewise, the 
regex library has no clue about R syntax. Your attempt failed to create a 
character string for grep to give to the regex library. To do that read 
?paste.

grep( paste( z, v, sep = "|" ), a )

On Thu, 22 Sep 2016, Fix Ace via R-help wrote:

> Hello, there,
> My patterns are defined by variables, for example:z="h"v="x"
> I have a vector: 
> a=c("th","mx","t")
> I would like to find elements in vector a that contain either "h", or "x"
> grep("h|x", a) apparently works. However, when I tried: grep(z|v,a), it did not work. Can anyone help how to handle such situation?
> Thanks.
> Ace 
>
>    On Wednesday, March 18, 2015 5:52 PM, Fix Ace <acefix at rocketmail.com> wrote:
> 
>
> Thank you very much!
> I do need to learn more about R!! 
>
>
>     On Tuesday, March 17, 2015 9:26 PM, William Dunlap <wdunlap at tibco.com> wrote:
> 
>
> Fix Ace wrote    What is the default "n"?
> 512:   > length(density(rnorm(10^6))$x)   [1] 512   > args(density.default)   function (x, bw = "nrd0", adjust = 1, kernel = c("gaussian",        "epanechnikov", "rectangular", "triangular", "biweight",        "cosine", "optcosine"), weights = NULL, window = kernel,        width, give.Rkern = FALSE, n = 512, from, to, cut = 3, na.rm = FALSE,        ...)   NULL   > ?density # or ?density.default, should also tell you about its meaning
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> On Tue, Mar 17, 2015 at 7:02 PM, Fix Ace <acefix at rocketmail.com> wrote:
>
> Thank you for the email.
> What is the default "n"?
> Thanks! 
>
>
>     On Tuesday, March 17, 2015 4:06 PM, William Dunlap <wdunlap at tibco.com> wrote:
> 
>
> Increasing the value of 'n' given to density will give an estimate at more points so it will look smoother.  Try n=2^18.
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> On Tue, Mar 17, 2015 at 12:06 PM, Fix Ace <acefix at rocketmail.com> wrote:
>
>
>
>  I have a dataset with 6187 elements, ranged from 3 to 104028. When I tried to examine only small range of data, I found that the plot was not smooth (as shown below):
> plot(density(test$V2), xlim=c(0,1000))
>
>
>  Is there away to make it smoother?
> Thanks a lot!!
>
>  
>
>
>  
>
>
>
>   
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
>
>
>
> 
>
>
>
> 
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------


More information about the R-help mailing list