I want to make sure this piece of code I wrote is doing what I want it to do. ll<-function(string) { grep(string,dir(),value=T) } subdir = ll("Coverage_[1-9][0-9]$") I basically wrote a little function that would grab all the files of form Coverage_[0-99] The way I wrote it, will it grab Coverage_5 or does it have to have 2 numbers (10-99)? -- View this message in context: http://www.nabble.com/Pattern-match-in-R-tp19743389p19743389.html Sent from the R help mailing list archive at Nabble.com.
> I want to make sure this piece of code I wrote is doing what I want itto do.> > ll<-function(string) > { > grep(string,dir(),value=T) > } > > > subdir = ll("Coverage_[1-9][0-9]$") > > I basically wrote a little function that would grab all the files ofform> Coverage_[0-99] > > The way I wrote it, will it grab Coverage_5 or does it have to have 2 > numbers (10-99)?This is straightforward to test. filenames <- paste("Coverage", 0:99, sep="_") grep("Coverage_[1-9][0-9]$", filenames, value=TRUE) This returns: [1] "Coverage_10" "Coverage_11" "Coverage_12" "Coverage_13" [5] "Coverage_14" "Coverage_15" "Coverage_16" "Coverage_17" ... [89] "Coverage_98" "Coverage_99" If you want all the files (Coverage_0 through to coverage_99), try this instead: grep("Coverage_[0-9]{1,2}$", filenames, value=TRUE) You may wish to take a look at the Repetition section on this page: http://www.regular-expressions.info/quickstart.html Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
On 30-Sep-08 14:36:04, bioinformatics_guy wrote:> I want to make sure this piece of code I wrote is doing what > I want it to do. > > ll<-function(string) > { > grep(string,dir(),value=T) > } > > subdir = ll("Coverage_[1-9][0-9]$") > > I basically wrote a little function that would grab all the > files of form Coverage_[0-99] > > The way I wrote it, will it grab Coverage_5 or does it have to have 2 > numbers (10-99)?I think you want "Coverage_[1-9]*[0-9]$", since your form will only match "Coverage_mn" where m is one of 1-9 and n is one of 0-9. The "*" means "zero or any number of ... ". Example (command-line grep in Linux): grep 'Coverage_[1-9]*[0-9]$' << EOT> Coverage > Coverage_5 > Coverage_01 > Coverage_19 > Coverage_19q > EOTCoverage_5 Coverage_19 Note that this does not catch "Coverage_01" because "[1-9]" does not include "0". Use "[0-9]" here as well if you need this. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 30-Sep-08 Time: 16:43:01 ------------------------------ XFMail ------------------------------
Try this where ? signifies that the prior character is optional: dir(pattern = "^Coverage_[1-9]?[0-9]$") On Tue, Sep 30, 2008 at 10:36 AM, bioinformatics_guy <wwwhitener at gmail.com> wrote:> > I want to make sure this piece of code I wrote is doing what I want it to do. > > ll<-function(string) > { > grep(string,dir(),value=T) > } > > > subdir = ll("Coverage_[1-9][0-9]$") > > I basically wrote a little function that would grab all the files of form > Coverage_[0-99] > > The way I wrote it, will it grab Coverage_5 or does it have to have 2 > numbers (10-99)? > > -- > View this message in context: http://www.nabble.com/Pattern-match-in-R-tp19743389p19743389.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >