[R] Storing results in a single file after looping over all files

Debs Majumdar debs_stata at yahoo.com
Thu Jun 28 07:49:40 CEST 2012


Hi All,

I have a whole lot of *.raw files in my working folder and I am doing the same analysis on each of those and want to save all the results in a single file. I am making some mistake here and can't figure out how to solve it.


Say, the *.raw files are ABCD.raw, EFGH.raw, IJKL.raw ...


The files are of this format

ID PHI   aa1  aa2  aa3 ....

1    1     1.3   2.0   1.0

2    0     1.5   NA   0.9

3    1     0.1   0.2   1.5

......
..


My code is as follows:

files <- list.files(pattern="*.raw")
for(i in files){
of <- strsplit(i, "\\.")[[1]]
of <- paste(head(of, 1))

data <- read.table(file=i, header=T)
y<-data$PHI

num<-length(y)
index1<-c(1:num_sample)[y==1]
index2<-c(1:num_sample)[y==0]
gen<-as.matrix(data[,-c(1:2)])
source("pcc.R") # a function for my use
out<- fpc_func(gen,num,index1,index2)


out1<-as.data.frame(out)
id1<-data[,2]
id<- as.data.frame(iid1)
out2<-cbind(iid1,out1)
colnames(out2)[2] <- of
}
write.table(out2, file="ALL.txt", append=T, col.names=T, row.names=F, quote=F, sep = "\t")

#######################

I can do it for each file separately but can't figure out how to store the results in one file.

If I do it for each file, the output I get

For ABCD.raw

id  ABCD

1     0.11
2    -0.24

3     2.18

........
......



For EFGH.raw

id  ABCD

1     -0.18
2     -1.33

3      0.88

........
......

 etc.

I would like to get the results in one file

id  ABCD   EFGH   IJKL .........

1     0.11     -0.18    ...........
2    -0.24    -1.33    ...........

3     2.18     0.88   .......

........
......

Thanks,

Debs




More information about the R-help mailing list