Hello, I have 2700 text files in a folder and need to apply the same program/procedure to each individually. I'm trying to find how to code something like: For each file in <Folder> do {<Procedure>} is there an easy way to do this? other suggestions? I have tried to list all the files names in a vector e.g.>listfiles[1:10,1]1 H:/Rtest/AXP.txt 2 H:/Rtest/BA.txt 3 H:/Rtest/C.txt 4 H:/Rtest/CAT.txt 5 H:/Rtest/DD.txt 6 H:/Rtest/DIS.txt 7 H:/Rtest/EK.txt 8 H:/Rtest/GE.txt 9 H:/Rtest/GM.txt 10 H:/Rtest/HD.txt but R doesn't like statements of type>read.table(file=listfiles[1,1])since 'file' must be a character string or connection... Any thoughts? Many thanks in advance, Ron Piccinini.
Ron Piccinini wrote:> Hello, > > I have 2700 text files in a folder and need to apply > the same program/procedure to each individually. I'm > trying to find how to code something like: > > For each file in <Folder> do {<Procedure>} > > is there an easy way to do this? other suggestions?files <- listfiles() results <- lapply(files, yourprocessing()) where yourprocessing is a function taking as argument a file name and returning whatever you want. Kjetil> > I have tried to list all the files names in a vector > e.g. > >> listfiles[1:10,1] > > 1 H:/Rtest/AXP.txt > 2 H:/Rtest/BA.txt > 3 H:/Rtest/C.txt > 4 H:/Rtest/CAT.txt > 5 H:/Rtest/DD.txt > 6 H:/Rtest/DIS.txt > 7 H:/Rtest/EK.txt > 8 H:/Rtest/GE.txt > 9 H:/Rtest/GM.txt > 10 H:/Rtest/HD.txt > > but R doesn't like statements of type > >> read.table(file=listfiles[1,1]) > > since 'file' must be a character string or > connection... > > Any thoughts? > > Many thanks in advance, > > Ron Piccinini. > > ______________________________________________ > 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 >
On 11/27/2005 3:51 PM, Ron Piccinini wrote:> Hello, > > I have 2700 text files in a folder and need to apply > the same program/procedure to each individually. I'm > trying to find how to code something like: > > For each file in <Folder> do {<Procedure>} > > is there an easy way to do this? other suggestions? > > I have tried to list all the files names in a vector > e.g. > > >>listfiles[1:10,1] > > > 1 H:/Rtest/AXP.txt > 2 H:/Rtest/BA.txt > 3 H:/Rtest/C.txt > 4 H:/Rtest/CAT.txt > 5 H:/Rtest/DD.txt > 6 H:/Rtest/DIS.txt > 7 H:/Rtest/EK.txt > 8 H:/Rtest/GE.txt > 9 H:/Rtest/GM.txt > 10 H:/Rtest/HD.txt > > but R doesn't like statements of type > > >>read.table(file=listfiles[1,1]) > > > since 'file' must be a character string or > connection... > > Any thoughts?From the look of it, the listfiles column that you created has been converted to a factor. You can convert back to character using as.character(); the as.is=TRUE parameter in the file reading functions will prevent the conversion in the first place, if that's how it happened. Then something like results <- list() for (f in as.character(listfiles[,1])) results[[f]] <- read.table(file=f) will read all the files and put them in a list. Duncan Murdoch