Dear All, I am given a set of files names as: velocity1.txt velocity2.txt and so on. I am sure there must be a way to read them automatically in R. It is really taking me longer to read them than to analyze them. Anybody has a suggestion to help me out with this? Many thanks Lorenzo
On Wed, 2006-10-18 at 17:09 +0200, Lorenzo Isella wrote:> Dear All, > I am given a set of files names as: > velocity1.txt > velocity2.txt > and so on. > I am sure there must be a way to read them automatically in R. > It is really taking me longer to read them than to analyze them. > Anybody has a suggestion to help me out with this? > Many thanksNot what you mean by "reading". ?read.table ?read.csv ?scan ... However, consider this example. If you have 100 files, you can do: for(i in 1:100) { fn <- paste("velocity",i,".txt",sep="") dat <- read.csv(fn) # ... do your stuff on "dat" here ... } HTH, Jerome -- Jerome Asselin, M.Sc., Agent de recherche, RHCE CHUM -- Centre de recherche 3875 rue St-Urbain, 3e etage // Montreal QC H2W 1V1 Tel.: 514-890-8000 Poste 15914; Fax: 514-412-7106
See ?read.table and the FAQs on importing data> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Lorenzo Isella > Sent: Wednesday, October 18, 2006 11:09 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Automatic File Reading > > Dear All, > I am given a set of files names as: > velocity1.txt > velocity2.txt > and so on. > I am sure there must be a way to read them automatically in R. > It is really taking me longer to read them than to analyze them. > Anybody has a suggestion to help me out with this? > Many thanks > > Lorenzo > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Just to complete: if you need them all at the same time: for(i in 1:100) { fn <- paste("velocity",i,".txt",sep="") varname <- paste("velocity",i,sep="") assign(varname,read.csv(fn)) } and you have a list of objects {velocity1, ..., velocity100} with corresponding data. Scionforbai
There have been many threads on this topic. The posting guide would suggest you do something like this before posting to the list:> RSiteSearch("reading many files")Which reveals many relevant threads, such as: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/84176.html http://finzi.psych.upenn.edu/R/Rhelp02a/archive/46661.html Chuck On Wed, 18 Oct 2006, Lorenzo Isella wrote:> Dear All, > I am given a set of files names as: > velocity1.txt > velocity2.txt > and so on. > I am sure there must be a way to read them automatically in R. > It is really taking me longer to read them than to analyze them. > Anybody has a suggestion to help me out with this? > Many thanks > > Lorenzo > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717
Forget about assign() & Co. Search R-help for 'assign', read the documentation on lists, and realize that it's quite a lot better to use lists for this kind of stuff.> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Scionforbai > Sent: Wednesday, October 18, 2006 12:04 PM > To: Lorenzo Isella > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Automatic File Reading > > Just to complete: if you need them all at the same time: > > for(i in 1:100) > { > fn <- paste("velocity",i,".txt",sep="") > varname <- paste("velocity",i,sep="") > assign(varname,read.csv(fn)) > } > > and you have a list of objects {velocity1, ..., velocity100} with > corresponding data. > > Scionforbai > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
is there a similar way to read all txt or csv files with same structure from a folder? thanks. On 10/18/06, Jerome Asselin <jerome.asselin at crchum.qc.ca> wrote:> On Wed, 2006-10-18 at 17:09 +0200, Lorenzo Isella wrote: > > Dear All, > > I am given a set of files names as: > > velocity1.txt > > velocity2.txt > > and so on. > > I am sure there must be a way to read them automatically in R. > > It is really taking me longer to read them than to analyze them. > > Anybody has a suggestion to help me out with this? > > Many thanks > > Not what you mean by "reading". > ?read.table > ?read.csv > ?scan > ... > > However, consider this example. If you have 100 files, you can do: > > for(i in 1:100) > { > fn <- paste("velocity",i,".txt",sep="") > dat <- read.csv(fn) > # ... do your stuff on "dat" here ... > } > > HTH, > Jerome > > -- > Jerome Asselin, M.Sc., Agent de recherche, RHCE > CHUM -- Centre de recherche > 3875 rue St-Urbain, 3e etage // Montreal QC H2W 1V1 > Tel.: 514-890-8000 Poste 15914; Fax: 514-412-7106 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- WenSui Liu (http://spaces.msn.com/statcompute/blog) Senior Decision Support Analyst Cincinnati Children Hospital Medical Center
Wensui Lui asks:> is there a similar way to read all txt or csv files with same > structure from a folder?On Windows I use this construct to find all files with the specified wild card name. I used the "\\" in the file paths with the translate=FALSE, because the "/" in the DOS switches "/w/B" must not be translated. On Windows this picks up both lower and upper case filenames A similar construct can be written for Unix. tmp <- shell('dir c:\\HOME\\rmh\\tmp\\*.R /w/B', intern=TRUE, translate=FALSE) ##msdos for (i in tmp) source(paste("c:\\HOME\\rmh\\tmp\\", i, sep=""))
Sorry... also, how do I remove the last line which says "end data" -- View this message in context: http://n4.nabble.com/R-Automatic-File-Reading-tp810331p1182386.html Sent from the R help mailing list archive at Nabble.com.