appending collums in for loop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

appending collums in for loop

Who Am I?
Hoi All,

I am trying to append collums to a data frame in a for loop. I read in tables, do some processing and then write the result to a data.frame. But, the thing I want is, that the results are appended to the data frame in stead of overwriting the results of the prevous table.
It has to look something like this:

After going trough the loop once:
Array 1
1
2
3
4
5

After going trough the loop twice:
Array 1 Array 2
1          1
2          2
3          3
4          4
5          5

After going trough the loop three times:
Array 1 Array 2 Array 3
1           1          1
2           2          2
3           3          3
4           4          4
5           5          5

This is my code:

setwd("J:/Stage/Datasets2/Datasets/outData")

masterTable<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls",sep="\t", dec=",", fill=T, header=T)
masterTable<-data.frame(masterTable)

fileNames<-list.files(getwd(), pattern='_0,5 -0,51.xls')
regionMatchABCDE<-data.frame()

for(i in 1:5) {
    fileName <- fileNames[i]
    newFile <- file.path(getwd(), paste(fileNames[i], "samen_0,5 -0,51.xls"))
    snpidFile<-read.table(fileNames[i],sep="\t", dec=",", fill=T, header=T)
    snpidFile<-data.frame(snpidFile)
    regionMatch<-cbind(masterTable, masterTable[match(masterTable$Pos, snpidFile$Pos),])
    regionMatchABCDE<-cbind(regionMatch[,10:18])
}

write.table(regionMatchABCDE, file= "Array 0-1-2-3-4-5.xls", col.names=T, row.names=F, quote=F, sep = "\t")

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: appending collums in for loop

Who Am I?
I forgot to say I need it to work in a for loop because it will be used for over 35 files. I previously programmed it in the most unorthodox way possible:

setwd("J:/Stage/Datasets2/Datasets/outData")
data1<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls",sep="\t", dec=",", fill=T, header=T)
bukuA<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
bukuB<-read.table("AR1000901A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
bukuC<-read.table("AR1000902A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
bukuD<-read.table("AR1000903A_N_291110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
bukuE<-read.table("AR1000904A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
bukuA<-data.frame(bukuA)
bukuB<-data.frame(bukuB)
bukuC<-data.frame(bukuC)
bukuD<-data.frame(bukuD)
bukuE<-data.frame(bukuE)
regionMatchA<-cbind(data1, bukuA[match(data1$Pos, bukuA$Pos),])
regionMatchB<-cbind(data1, bukuB[match(data1$Pos, bukuB$Pos),])
regionMatchC<-cbind(data1, bukuC[match(data1$Pos, bukuC$Pos),])
regionMatchD<-cbind(data1, bukuD[match(data1$Pos, bukuD$Pos),])
regionMatchE<-cbind(data1, bukuE[match(data1$Pos, bukuE$Pos),])


regionMatchABCDE<-cbind(data1[,7],data1[,3], regionMatchA[,10:18],regionMatchB[,10:18],regionMatchC[,10:18],regionMatchD[,10:18],regionMatchE[,10:18])
write.table(regionMatchABCDE, file= "Array0-1-2-3-4-5_0,5 -0,5.xls", append=F, col.names=T, row.names=F, quote=F, sep = "\t", dec=",")
Reply | Threaded
Open this post in threaded view
|

Re: appending collums in for loop

ssefick
I don't think I understand.  Can you work up a dummy example that will
run independent of your actual data, and produce the problem?  This
will help everyone diagnose the problem.  Naively this sounds like an
indexing problem.

Stephen

On Mon, Mar 21, 2011 at 7:56 AM, Who Am I? <[hidden email]> wrote:

> I forgot to say I need it to work in a for loop because it will be used for
> over 35 files. I previously programmed it in the most unorthodox way
> possible:
>
> setwd("J:/Stage/Datasets2/Datasets/outData")
> data1<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls",sep="\t", dec=",", fill=T, header=T)
> bukuA<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
> bukuB<-read.table("AR1000901A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
> bukuC<-read.table("AR1000902A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
> bukuD<-read.table("AR1000903A_N_291110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
> bukuE<-read.table("AR1000904A_N_241110_(Mapping250K_Nsp),Mapping250K_Nsp,CNprobes.tab
> _SNP_IDs.xls _0,5 -0,51.xls", sep="\t", dec=",", fill=T, header=T)
> bukuA<-data.frame(bukuA)
> bukuB<-data.frame(bukuB)
> bukuC<-data.frame(bukuC)
> bukuD<-data.frame(bukuD)
> bukuE<-data.frame(bukuE)
> regionMatchA<-cbind(data1, bukuA[match(data1$Pos, bukuA$Pos),])
> regionMatchB<-cbind(data1, bukuB[match(data1$Pos, bukuB$Pos),])
> regionMatchC<-cbind(data1, bukuC[match(data1$Pos, bukuC$Pos),])
> regionMatchD<-cbind(data1, bukuD[match(data1$Pos, bukuD$Pos),])
> regionMatchE<-cbind(data1, bukuE[match(data1$Pos, bukuE$Pos),])
>
>
> regionMatchABCDE<-cbind(data1[,7],data1[,3],
> regionMatchA[,10:18],regionMatchB[,10:18],regionMatchC[,10:18],regionMatchD[,10:18],regionMatchE[,10:18])
> write.table(regionMatchABCDE, file= "Array0-1-2-3-4-5_0,5 -0,5.xls",
> append=F, col.names=T, row.names=F, quote=F, sep = "\t", dec=",")
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/appending-collums-in-for-loop-tp3393446p3393481.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Stephen Sefick
____________________________________
| Auburn University                                         |
| Biological Sciences                                      |
| 331 Funchess Hall                                       |
| Auburn, Alabama                                         |
| 36849                                                           |
|___________________________________|
| [hidden email]                                  |
| http://www.auburn.edu/~sas0025                 |
|___________________________________|

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

                                -K. Mullis

"A big computer, a complex algorithm and a long time does not equal science."

                              -Robert Gentleman

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: appending collums in for loop

Who Am I?
In reply to this post by Who Am I?
Got it working with a package called 'xlsReadWritePro' (which requires a license after 30 day's...) This is my code:
setwd("J:/Stage/Datasets2/Datasets/outData")

masterTable<-read.table("AR1000900A_N_241110_(Mapping250K_Nsp)_2,Mapping250K_Nsp,CNprobes.tab _SNP_IDs.xls",sep="\t", dec=",", fill=T, header=T)
masterTable<-data.frame(masterTable)

fileNames<-list.files(getwd(), pattern='_0,5 -0,51.xls')
library(xlsReadWritePro)
regionMatchM<-data.frame()
write.xls(regionMatchM, file= "Array012345.xls")                                                              
for(i in 1:5) {
                fileName <- fileNames[i]
    newFile <- file.path(getwd(), paste(fileNames[i], "samen_0,5 -0,51.xls"))
                snpidFile<-read.table(fileNames[i],sep="\t", dec=",", fill=T, header=T)
    snpidFile<-data.frame(snpidFile)
                regionMatch<-cbind(masterTable, snpidFile[match(masterTable$Pos, snpidFile$Pos),])
    regionMatchABCDE<-data.frame(regionMatch[10:18])
    write.xls(regionMatchABCDE, file= "Array012345.xls", naStrings = "NA", append=T, from= c(1,(1+i)*9), rowNames=FALSE)
}
genomeInfo<-read.xls("Array012345.xls")
len<-length(genomeInfo[1,])
finalTable<-data.frame(masterTable, genomeInfo[,18:len])
summary(finalTable)
write.table(finalTable, file= "Array012345_final1.xls", append=F, col.names=T, row.names=F, quote=F, sep = "\t")

Thanks for the help anyway. If someone has a solution for this problem, without the need of a package, please reply