Dear R experts, I have a folder names "200209" and in this folder there are many data files, such as: "BA020902.txt","BA020903.txt","BA020904.txt", "BA020905.txt","BA020906.txt","BA020909.txt", "BA020910.txt","BA020911.txt","BA020912.txt", "BA020913.txt","BA020916.txt","BA020917.txt", "BA020918.txt","BA020919.txt","BA020920.txt", "BA020923.txt","BA020924.txt","BA020925.txt", "BA020926.txt","BA020927.txt","BA020930.txt", "GMAS0209.txt","MAST0209.txt" I want to imort all these data files into R at once without typing tedious a<-read.table("BA020902.txt"), b<-read.table("BA020903.txt")..... Thanks Ted -- View this message in context: http://www.nabble.com/batch-process-file-in-R-tp22474751p22474751.html Sent from the R help mailing list archive at Nabble.com.
tedzzx wrote:> Dear R experts, > > I have a folder names "200209" and in this folder there are many data files, > such as: > "BA020902.txt","BA020903.txt","BA020904.txt", > "BA020905.txt","BA020906.txt","BA020909.txt", > "BA020910.txt","BA020911.txt","BA020912.txt", > "BA020913.txt","BA020916.txt","BA020917.txt", > "BA020918.txt","BA020919.txt","BA020920.txt", > "BA020923.txt","BA020924.txt","BA020925.txt", > "BA020926.txt","BA020927.txt","BA020930.txt", > "GMAS0209.txt","MAST0209.txt" > > I want to imort all these data files into R at once without typing tedious > a<-read.table("BA020902.txt"), > b<-read.table("BA020903.txt")..... > > Thanks > > Ted >How about something like that: out <- sapply( dir( pattern = "\\.txt$" ), read.table ) Romain -- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
Dear Ted, Try this: # Working directory -- files are located here setwd("C:\\200209") # Names of the files Files<-c( paste("BA02090",c(2:6,9),".txt",sep=""), paste("BA0209",c(10:13,16:20,23:27,30),".txt",sep=""), "GMAS0209.txt","MAST0209.txt" ) Files # Reading the files into R Reads<-sapply(Files,function(x) read.table(x,header=TRUE)) # you can change this ;) Reads Note that "Reads" will be a list which each component represents one of the ".txt" files listed in "Files". See ?setwd, ?sapply, ?paste and ?read.table for more details. HTH, Jorge On Thu, Mar 12, 2009 at 7:52 AM, tedzzx <zengzhenxing@gmail.com> wrote:> > Dear R experts, > > I have a folder names "200209" and in this folder there are many data > files, > such as: > "BA020902.txt","BA020903.txt","BA020904.txt", > "BA020905.txt","BA020906.txt","BA020909.txt", > "BA020910.txt","BA020911.txt","BA020912.txt", > "BA020913.txt","BA020916.txt","BA020917.txt", > "BA020918.txt","BA020919.txt","BA020920.txt", > "BA020923.txt","BA020924.txt","BA020925.txt", > "BA020926.txt","BA020927.txt","BA020930.txt", > "GMAS0209.txt","MAST0209.txt" > > I want to imort all these data files into R at once without typing tedious > a<-read.table("BA020902.txt"), > b<-read.table("BA020903.txt")..... > > Thanks > > Ted > > > -- > View this message in context: > http://www.nabble.com/batch-process-file-in-R-tp22474751p22474751.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
If these are the only files in the directory, then you might try... (File paths will need to change if the folder "200209" isn't in the working directory) fpath <- "./200209" a <- list.files(fpath) for(i in 1:length(a)){ assign(paste("y",i,sep=""),read.table(paste(fpath,a[i],sep="/"))) } Another option is to put them in a list... for(i in 1:length(a)){ y[[i]] <- read.table(paste(fpath,a[i],sep="/") } or with lapply... y <- lapply(1:length(a), function(i) read.table(paste(fpath,a [i],sep="/")) Hope this helps. Cheers, Derek On Mar 12, 7:52?am, tedzzx <zengzhenx... at gmail.com> wrote:> Dear R experts, > > I have a folder names "200209" and in this folder there are many data files, > such as: > "BA020902.txt","BA020903.txt","BA020904.txt", > "BA020905.txt","BA020906.txt","BA020909.txt", > "BA020910.txt","BA020911.txt","BA020912.txt", > "BA020913.txt","BA020916.txt","BA020917.txt", > "BA020918.txt","BA020919.txt","BA020920.txt", > "BA020923.txt","BA020924.txt","BA020925.txt", > "BA020926.txt","BA020927.txt","BA020930.txt", > "GMAS0209.txt","MAST0209.txt" > > I want to imort all these data files into R at once without typing tedious > a<-read.table("BA020902.txt"), > b<-read.table("BA020903.txt")..... > > Thanks > > Ted > > -- > View this message in context:http://www.nabble.com/batch-process-file-in-R-tp22474751p22474751.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.