[R] Pattern match in R

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue Sep 30 17:26:24 CEST 2008


> 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)?

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



More information about the R-help mailing list