[R] 'For each file in folder F do....'

Kjetil Brinchmann Halvorsen kjetilbrinchmannhalvorsen at gmail.com
Sun Nov 27 22:19:35 CET 2005


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
>




More information about the R-help mailing list