Question

So I am still an R newb so be gentle ;) I basically should be able to figure this out but I think frustration has set in. I wish to simply write three columns to a file using write.table (easy enough) but I also wish that it is appended via "\t" separator so my resulting file is a tab delimited txt file. I have attached my code which works fine but they are appended via rows so I would love to be able to solve this. Any pointers would be awesome!

Cheers

for(num in 0:10){
  input<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\HMEC(p9)\\Exp*_HMEC(p9)-IP",num,".txt",sep="")
  files<-Sys.glob(input)
  outfile<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\exps_HMEC(p9)-IP",num,".txt",sep="")
    exp1<-read.table(files[1],header=TRUE,sep="\t")
    e1<-exp1[grep("9", exp1$Chromosome, invert=TRUE), ]
    write.table(e1$Probe,row.names=FALSE,col.names=FALSE,quote=FALSE,append=TRUE,outfile)
    exp2<-read.table(files[2],header=TRUE,sep="\t")
    e2<-exp2[grep("9", exp2$Chromosome, invert=TRUE), ]
    write.table(e2$Probe,row.names=FALSE,col.names=FALSE,quote=FALSE,append=TRUE,outfile)
    exp3<-read.table(files[3],header=TRUE,sep="\t")
    e3<-exp3[grep("9", exp3$Chromosome, invert=TRUE), ]
    write.table(e3$Probe,row.names=FALSE,col.names=FALSE,quote=FALSE,append=TRUE,outfile)    

}
Was it helpful?

Solution

Put everything you want to output into a single data.frame, then use write.table() with the sep="\t" argument:

for(num in 0:10){
  input<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\HMEC(p9)\\Exp*_HMEC(p9)-IP",num,".txt",sep="")
  files<-Sys.glob(input)
  outfile<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\exps_HMEC(p9)-IP",num,".txt",sep="")
    exp1<-read.table(files[1],header=TRUE,sep="\t")
    exp2<-read.table(files[2],header=TRUE,sep="\t")
    exp3<-read.table(files[3],header=TRUE,sep="\t")

    e1<-exp1[grep("9", exp1$Chromosome, invert=TRUE), ]
    e2<-exp2[grep("9", exp2$Chromosome, invert=TRUE), ]
    e3<-exp3[grep("9", exp3$Chromosome, invert=TRUE), ]

    tableData <- cbind(e1$Probe, e2$Probe, e3$Probe)

    write.table(tableData,file=outfile,sep="\t",row.names=FALSE,col.names=FALSE,quote=FALSE,append=TRUE)


}

OTHER TIPS

Ok so serious credit to Mason DeCamillis!!! Here is the solution that worked for me... It may not look pretty but it does the trick :) Thanks again!

for(num in 0:10){
  input<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\HMEC(p9)\\Exp*_HMEC(p9)-IP",num,".txt",sep="")
  files<-Sys.glob(input)
  outfile<-paste("C:\\Users\\Desktop\\non-viewpoint 20-2-14\\exps_HMEC(p9)-IP",num,".txt",sep="")
    exp1<-read.table(files[1],header=TRUE,sep="\t")
    e1<-exp1[grep("9", exp1$Chromosome, invert=TRUE), ]

    exp2<-read.table(files[2],header=TRUE,sep="\t")
    e2<-exp2[grep("9", exp2$Chromosome, invert=TRUE), ]

    exp3<-read.table(files[3],header=TRUE,sep="\t")
    e3<-exp3[grep("9", exp3$Chromosome, invert=TRUE), ]


    e1$id <- 1:nrow(e1)
    e1<-cbind(e1,e1$id)
    e2$id <- 1:nrow(e2)
    e2<-cbind(e2,e2$id)
    e3$id <- 1:nrow(e3)
    e3<-cbind(e3,e3$id)
    tableData <- merge(e1, e2, by="id", all=TRUE)
    tableData <- merge(tableData, e3, by="id", all=TRUE)

    ire<-data.frame(tableData$Probe.x)
    eng<-data.frame(tableData$Probe.y)
    fra<-data.frame(tableData$Probe)

    jondata<-cbind(ire,eng,fra)
    write.table(jondata,row.names=FALSE,col.names=FALSE,quote=FALSE,append=TRUE,outfile,sep="\t")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top