[R] 'For each file in folder F do....'
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 2H:/Rtest/BA.txt 3 H:/Rtest/C.txt 4 H:/Rtest/CAT.txt 5H:/Rtest/DD.txt 6 H:/Rtest/DIS.txt 7H:/Rtest/EK.txt 8H:/Rtest/GE.txt 9H:/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@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
Re: [R] 'For each file in folder F do....'
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 2H:/Rtest/BA.txt 3 H:/Rtest/C.txt 4 H:/Rtest/CAT.txt 5H:/Rtest/DD.txt 6 H:/Rtest/DIS.txt 7H:/Rtest/EK.txt 8H:/Rtest/GE.txt 9H:/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@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 __ R-help@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
Re: [R] 'For each file in folder F do....'
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 2H:/Rtest/BA.txt 3 H:/Rtest/C.txt 4 H:/Rtest/CAT.txt 5H:/Rtest/DD.txt 6 H:/Rtest/DIS.txt 7H:/Rtest/EK.txt 8H:/Rtest/GE.txt 9H:/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 __ R-help@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