[R] merge tables in a loop

clemens karwautz clemdawg at gmx.net
Wed Jan 26 11:51:56 CET 2011


I’m running a loop opening one file after another. 

>setwd("D:/Documents and Settings/trflp")
>a<-list.files()

>results.diversity<-data.frame(matrix(0,ncol=7,nrow=length(a)))
>names(results.diversity)<-c("file","simpson","shannon","eveness")

>x<-length(a)
>for (i in 1:x){
> trflp<-read.table(a[i],header=T,sep="\t")
…

I was able to make a table with the results of my calculations for each file.

>results.diversity$simpson[i]<-simpson
>results.diversity$shannon[i]<-shannon
>results.diversity$eveness[i]<-eveness
>write.table(results.diversity,"diversity.txt",row.names=F,sep="\t")

Now, I would be interested in writing a table with several rows per file.

e.g.:
file1:
size	abundance
37	0.0117
43	0.1566
218	0.0682
253	0.0508
412	0.0874
...

file2:
size	abundance
37	0.0117
45	0.1876
218	0.0682
255	0.0808
417	0.0374
...

Final table:
size	abundance	filename
37	0.0117 	file1
43	0.1566 	file1
218	0.0682 	file1
253	0.0508 	file1
412	0.0874 	file1
37	0.0117 	file2
45	0.1876 	file2
218	0.0682 	file2
255	0.0808 	file2
417	0.0374 	file2

Could you give me some advise how to manage this problem?
Thank you very much

Clemens

-- 
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit 
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl



More information about the R-help mailing list