On 2015-01-07 , at 16:29, Haznadar, Majda (NIH/NCI) [E] 
<majda.hazna...@nih.gov> wrote:

> Hi,
> 
> I would like to loop a specific code through multiple files ending in .CDF in 
> the same directory, but I am unsure of how to do that (new to R).
> 
> Can someone please help me with this-thank you.
> 
> This is the code I would like to loop:
> 
>> xr<-xcmsRaw("mh141210xg2_sccs_00101.CDF", profstep=0)
>> write.mzdata(xr, file="mh141210xg2_sccs_00101.CDF")
> 
> And there are 675 of these .CDF files in the directory that I would like to 
> apply the code to. Also, when I save the file in the second line of code, I 
> would like it to have the same name as it previously had, so just re-write 
> the old file.
> 
> Thanks so much for your time!!
> Majda
> 




I use this function to find the appropriate file names, as follows:

getFolder <- function(pat)
        {
        txt=file.choose()
        #cat(txt,'\n')
        pos=0
        fname=basename(txt)
        #cat(paste("\nFilename found is: ",fname,'\n'))
        foln=dirname(txt)
        cat(paste("\nFolder name found is: ",foln,'\n'))
        drtext=dir(foln, pattern=pat, full.names = TRUE)
        #cat('\n\n\n\n') 
        return(drtext)
        }

# it can be called with the appropriate extension as a pattern, e.g. .wav
follist=getFolder("\\.wav")

#  this variable now contains all filenames (complete path names) of the 
selected type
# and can be used t read in a loop:

#  for (i in 1:length(follist) {
        #  rad ypur file
        #  do something with it
        #  view or save the results
        #  }

Maybe you can adapt this to your problem.

Success and
Best wishes,



Franklin Bretschneider
Dept of Biology
Utrecht University
brets...@xs4all.nl

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to