[R] Storing results in a single file after looping over all files
Debs Majumdar
debs_stata at yahoo.com
Fri Jun 29 00:35:46 CEST 2012
Hi,
I can upload the toy datasets and code but am unsure about a good hosting site. Suggestions?
I got the following error after the following command: out2 <- do.call(cbind, outs)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 750
### I have 750 unique IDs in each of the raw datasets.
Thanks,
Debs
________________________________
From: Jean V Adams <jvadams at usgs.gov>
To: Debs Majumdar <debs_stata at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Thursday, June 28, 2012 12:05 PM
Subject: Re: [R] Storing results in a single file after looping over all files
Without providing data and code that I can actually run
and test,
I can only take a guess at how to solve this.
See my edits and suggestions in the code, below.
Hope it helps.
Jean
Debs Majumdar <debs_stata at yahoo.com> wrote on
06/28/2012 12:49:40 AM:
> 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")
# before your for() loop, create an empty list to
put your results in
outs <- vector("list", length(files))
> 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)
# then in the next two lines, change out2 to outs[[i]]
outs[[i]]<-cbind(iid1,out1)
colnames(outs[[i]])[2] <- of
> out2<-cbind(iid1,out1)
> colnames(out2)[2] <- of
> }
# after the for() loop, combine all data.frames by
column
out2 <- do.call(cbind, outs)
> 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