[R] listing the files in a directory using regular expressions

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 14 11:41:01 CEST 2012


On Sun, Oct 14, 2012 at 3:13 AM, siddu479 <onlyfordigitalstuff at gmail.com> wrote:
> Hi Experts,
>
>   This might be silly question that I am asking, but no way as I am new to
> R.
> I want to list the files in a directory using regular expression like
> A_B*_C*.csv  etc.
> How to make this possible in R ?
> I tried like this list.files(dir=".", pattern="A_B*_C*.csv") but this gives
> no output, whereas list.files(.... pattern="*.csv") giving all the .csv
> files in the output, which I do not want.
>

1. Always read the help file:

?list.files

The argument name is path=, not dir=.

2. Also note that the pattern must be specified as a regular
expression and not a glob (as would be specified in a command line
shell).  For example, the B* part of the expression in your post means
0 or more B's.  It does not mean B followed by anything else.

If you want to use globs rather than regular expressions see

?glob2rx

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com




More information about the R-help mailing list