[R] Re: dir() and RegEx and gsub()

Sarah Goslee sarah.goslee at gmail.com
Thu Jun 9 19:09:00 CEST 2005


Hi,

> The command "dir( fold, pattern = "\.csv" )" gives back *all* the 3 files 
> With dir( fold, pattern = "\\.csv" ) I get back only VorlageTradefile.csv. 
> I don't understand this behaviour, IMHO the regex expression "\.csv"
> becomes the string ".csv" and "\\.csv" becomes "\.csv". So the first
> string should catch it. This is also consistent with the result when I
> tried with the TRegExpr Tool. Could somebody explain what's going on
> here?

Under R, for reasons I've never quite understood,

"\\." evaluates to . 
(the double backslash is needed to match a single period)
while "\." is the single-character wild card (which seems to be the same as ".")

This is explained in ?strsplit but not in the help for other commands
that use regex.

> b)
> I need to handle a copied windows file path. This is certainly often
> asked but I didn't find a solution.

If you mean you want to change the "\" to either "\\" or "/" I'm
really not sure.
On my linux system, it doesn't seem possible (which I'm sure is wrong). I'd
try sub() or at worst a combination of strsplit() and paste(), except that:

> test <- "D:\UebungenNDK\DataMining\DataMiningSeries.r"
> test
[1] "D:UebungenNDKDataMiningDataMiningSeries.r"

Regular expressions under R have always perplexed me just a bit. When
I've run into problems of this sort, I've always just processed the
strings in vim or similar, rather
than fight with R. I'm sure someone here understands them - hopeully
we will both
be enlightened.

Sarah

-- 
Sarah Goslee 
http://www.stringpage.com




More information about the R-help mailing list