[R] help with looping a specific code

Franklin Bretschneider bretschr at xs4all.nl
Wed Jan 7 18:21:49 CET 2015


On 2015-01-07 , at 16:29, Haznadar, Majda (NIH/NCI) [E] <majda.haznadar at 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
bretschr at xs4all.nl



More information about the R-help mailing list