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

2009-05-12 Thread Wacek Kusnierczyk
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
   



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


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

2009-05-11 Thread Andreas Christoffersen
I really dont know hot to use it - but have a look at ?source

On Mon, May 11, 2009 at 10:37 PM, Mark Na mtb...@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.

 Thanks!

 Mark Na

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


[[alternative HTML version deleted]]

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


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

2009-05-11 Thread Romain Francois


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.


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

2009-05-11 Thread Mose
Woo, global jinx!

On Mon, May 11, 2009 at 1:55 PM, Romain Francois romain.franc...@dbmail.com
 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.


[[alternative HTML version deleted]]

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


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

2009-05-11 Thread baptiste auguie

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.


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

2009-05-11 Thread Rolf Turner


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 mtb...@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@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.