Skip to content

'For each file in folder F do....'

3 messages · Ron Piccinini, Kjetil Halvorsen, Duncan Murdoch

#
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.
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
since 'file' must be a character string or
connection...

Any thoughts?

Many thanks in advance,

Ron Piccinini.
#
Ron Piccinini wrote:
files <- listfiles()
results <- lapply(files, yourprocessing())

where yourprocessing is a function taking as argument a file name and 
returning whatever you want.

Kjetil
#
On 11/27/2005 3:51 PM, Ron Piccinini wrote:
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