[R] read

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Nov 29 02:20:11 CET 2016

No, and yes, depending what you mean. 

No, because you have to supply the file name to open it... you cannot directly use wildcards to open files.

Yes,  because the list.files function can be used to match all file names fitting a regex pattern, and you can use those filenames to open the files.


fns  <- list.files( pattern="dat(\\.[^.]+)$" )
dtaL <- lapply( fns, function(fn){ read.csv( fn, stringsAsFactors=FALSE ) } )

If you only expect one file to be in any given directory, you can skip the lapply and just read the file, or you can extract the data frame from the list using dtaL[[ 1 ]].

?regex for help on patterns
Sent from my phone. Please excuse my brevity.

On November 28, 2016 2:23:23 PM PST, Ashta <sewashm at gmail.com> wrote:
>Hi all,
>I have a script that  reads a file (dat.csv)  from several folders.
>However, in some folders the file name is (dat) with out csv  and in
>other folders it is dat.csv.  The format of data is the same(only the
>file name differs  with and without "csv".
>Is it possible to read these files  depending on their name in one?
>like read.csv("dat.csv"). How can I read both type of file names?
>Thank you in advance
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list