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

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue May 12 11:51:13 CEST 2009


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 )
> }

or maybe

    csvs = Map(read.csv, dir(pattern='\\.csv$'))

possibly with a correction of list item names

    names(csvs) = sub('\\.csv$', '', names(csvs))

which is optional, since the content of foo.csv can be accessed as
csvs$foo (or as csvs[['foo', exact=FALSE]]) instead of csvs$foo.cvs even
without such a correction.

vQ

>
> 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
>>   
>
>




More information about the R-help mailing list