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

Rui Barradas ruipbarradas at sapo.pt
Thu Jun 28 22:26:19 CEST 2012


Hello,

You should post your data in a readable format.
Use ?dput.

Now, for your question, start by putting your results in a list.


abcd <- read.table(text="
id  ABCD
1     0.11
2    -0.24
3     2.18
", header=TRUE)

efgh <- read.table(text="
id  EFGH
1     -0.18
2     -1.33
3      0.88
", header=TRUE)

ijkl <- read.table(text="
id  IJKL
1      0.81
2     -1.03
3      1.38
", header=TRUE)

(df.list <- list(abcd, efgh, ijkl))

You now have two ways.
1. Assuming that all data.frames have the same 'id' values:

result <-do.call(cbind, list(df.list[[1]], lapply(df.list[-1], `[`, 2)))

2. If they do not, or if it's not certain:

result <- df.list[[1]]
lapply(df.list[-1], function(x){ result <<- merge(result, x); NULL })


And all you have to do is write.table().

Hope this helps,

Rui Barradas
Em 28-06-2012 06:49, Debs Majumdar escreveu:
> 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
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



More information about the R-help mailing list