[R] Reading multiple files

Bretschneider SIG-R bretschr at xs4all.nl
Wed Sep 26 23:43:37 CEST 2012


On 26 Sep 2012, at 16:11 , Silvano Cesar da Costa wrote:

> Hi,
> 
> I have 35 data files for reading. I would like get a program for
> performing reading of 35 files at once.
> All are of the type: Dados1.raw, Dados2.raw and so on.
> 
> If the files have the same number of columns, I can read with the
> following commands:
> 
> rm(list=ls())
> filenames = list.files(path="~/Silvano/Arq", pattern="Dados+.*raw")
> names = substr(filenames, 1, 7)
> 
> for(i in names){
>  filepath = file.path("~/Silvano/Dados", paste(i, ".raw", sep=""))
>  assign(i, read.delim(filepath,
>                       colClasses=c(rep("character", 5), rep("numeric", 5)),
>                       sep = ""))
> }
> 
> It happens that the files have different number of columns. And I can't
> solve the problem.
> 
> Any suggestions?
> 



Dear Silvano Cesar da Costa,


You can read an entire folder by analysing the path.
I wrote this program to convert wave files, but it can be applied to any file type.
Just change the few lines ("if ywave... etc) into your own operations:


#  select FOLDER with WAV-files
fnam = dirname(file.choose())
print(""); print("FILES")
print(list.files(fnam))
print(""); print("DIRS")
print(list.dirs(fnam, full.names=FALSE))
print(""); print("RECURSIVE FILE LIST")
filist = list.files(fnam, recursive=TRUE, pattern="wav")
print(filist)
filist1 = paste(fnam,"/",filist, sep="")
nfiles = length(filist1)
#  
#  filenames loop ===============================>>>>>>>>>>
for(i in 1:nfiles) {
	inname=filist1[i]
	ywave=readWave(inname)
	ster=ywave at stereo
	if (ster=="stereo") {} #  wat dan??
	srate=ywave at samp.rate
	ywave2=ywave
	if (ywave2 at bit==8) {
			ywave2 at left=(ywave2 at left-128)*255
			ywave2 at bit <- 16
			}
	if (ywave2 at samp.rate<100000) {
			ywave2 at samp.rate <- ywave2 at samp.rate * 10
			}
	if (ywave2 at samp.rate != 312500) {
			ywave2 <- resamp(ywave2,f=ywave2 at samp.rate,g=312500, output="Wave")
			}
	
	outname=paste(dirname(inname), "/*",basename(inname), sep="")
	writeWave(ywave2,outname)
	}


Best wishes,

Franklin Bretschneider

Dept of Biology
Kruytgebouw W711
Padualaan 8
3584 CH Utrecht
The Netherlands
f.bretschneider at uu.nl




More information about the R-help mailing list