Hi,

I once made this function (essentially the same as Romain),


assignFiles <-
function (pattern = "csv", strip = "(_|.csv|-| )", ...) # strip is any pattern you want to remove from the filenames
{
    listFiles <- list.files(pattern = pattern, all.files = FALSE,
        full.names = FALSE, recursive = FALSE)
    variables <- gsub(strip, "", listFiles)
    for (ii in (1:length(listFiles))) {
        name <- listFiles[ii]
        assign(variables[ii], read.csv(name, ...), env = .GlobalEnv)
    }
}


HTH,

baptiste

On 11 May 2009, at 22:55, Romain Francois wrote:


Hi,

Something like this perhaps:

files <- dir( pattern  = "\\.csv$" )
for( x in files){
   assign( sub( "\\.csv$", "", x ) , read.csv(x), envir = .GlobalEnv )
}

Romain

Mark Na wrote:
Hi R-helpers,

I would like to read into R all the .csv files that are in my working
directory, without having to use a read.csv statement for each file.

Each .csv would be read into a separate dataframe which would acquire
the filename of the .csv.

As an example:

Mark<-read.csv("Mark.csv")

...but the code (or command) would do this automatically for every
.csv in the working directory without my specifying each file.

I'd appreciate any help or ideas you might have.

Thanks!

Mark Na



--
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr

______________________________________________
R-help@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.

_____________________________

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

______________________________________________
R-help@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.

Reply via email to