Re: [R] Read many .csv files into an R session automatically, without specifying filenames

From: Rolf Turner <r.turner_at_auckland.ac.nz>
Date: Tue, 12 May 2009 09:13:08 +1200

On 12/05/2009, at 8:41 AM, Andreas Christoffersen wrote:

> I really dont know hot to use it - but have a look at ?source

        No; ``source'' is irrelevant here.
>
> On Mon, May 11, 2009 at 10:37 PM, Mark Na <mtb954@gmail.com> 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.

datalist <- list()
files <- list.files(pattern="\\.csv$")

	for(file in files) {
	stem <- gsub("\\.csv$","",file)
	datalist[[stem]] <- read.csv(file)

}

The foregoing does things the *right* way, putting the resulting data sets together
into a list whereby they can be accessed in an organized fashion. If you wrong-headedly
*insist* on putting the data sets separately into your workspace then do:

files <- list.files(pattern="\\.csv$")

	for(file in files) {
	stem <- gsub("\\.csv$","",file)
	assign(stem,read.csv(file))

}

        cheers,

                Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

R-help_at_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. Received on Mon 11 May 2009 - 20:17:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 11 May 2009 - 20:32:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive