0

I am trying to process some files which have gene annotation columns in them. I want to output a dataframe from each iteration of the loop I have so that eventually I can work with each dataframe separately (ideally the dataframe should be called the name of the file it comes from). I have attached what my working function but I don't know how to output named dataframes from each iteration.

library(gridExtra)


#LP6007427.DNA_PolyATClusters<-read.delim("U:\\Batch1\\LP6007427-DNA_PolyATClusters.csv",sep=",")
setwd("U:/Batch1/")
path = "U:\\Batch1\\"
path2 = "U:\\bed_extract\\"
filename <- dir(path, pattern =".csv")

pltList <- list()

for(i in 1:length(filename)){


  file <- read.delim(filename[i],header=FALSE,sep=',')
  row.names(file)<-NULL


  file<-file[-c(1:25),]
  write.table(i)


  Pusung<-data.frame(file[1],file[2],file[3],file[4],file[8])
  names(Pusung)<-c("chr","Start","End","Type","Gene")

  library(stringr)
  Pusung$chr<-gsub("chr","",Pusung$chr)
  Pusung$chr <- factor(as.integer(Pusung$chr), levels=unique(Pusung$chr))
  Pusung$Type<-gsub("J","Barr",Pusung$Type)
  Pusung$Type<-gsub("T","Tumour",Pusung$Type)

  PusungTumour <- as.data.frame(Pusung[grep("Barr", Pusung$Type, invert=TRUE), ])
  PusungShared<-as.data.frame(subset(Pusung,grepl('Barr', Type) & grepl('Tumour', Type)))
  PusungBarr <- as.data.frame(Pusung[grep("Tumour", Pusung$Type, invert=TRUE), ])

  PusungTumour$Type<-gsub('.*',"Tumour",PusungTumour$Type)
  PusungTumour[PusungTumour==""]<-NA
  Intergenic_Tum<-subset(PusungTumour,!is.na(PusungTumour$Gene))
  Intragenic_Tum<-subset(PusungTumour,is.na(PusungTumour$Gene))


  PusungBarr$Type<-gsub('.*',"Barr",PusungBarr$Type)
  PusungBarr[PusungBarr==""]<-NA
  Intergenic_Barr<-subset(PusungBarr,!is.na(PusungBarr$Gene))
  Intragenic_Barr<-subset(PusungBarr,is.na(PusungBarr$Gene))


  PusungShared$Type<-gsub('.*',"Shared",PusungShared$Type)
  PusungShared[PusungShared==""]<-NA
  Intergenic_Shared<-subset(PusungShared,!is.na(PusungShared$Gene))
  Intragenic_Shared<-subset(PusungShared,is.na(PusungShared$Gene))

  PusungBound<-rbind(Intragenic_Barr,Intragenic_Tum)
  PusungBound<-rbind(PusungBound,Intragenic_Shared)
}


do.call(grid.arrange, pltList)
  • 2
    Put them in a list, just like you put your plots in a list. [See here for tips](http://stackoverflow.com/a/24376207/903061). Or by "output" do you mean you want files? – Gregor Thomas Sep 02 '15 at 17:03
  • Having trouble following your code. At the end, you use `pltList`, but where do you create this list?? – jlhoward Sep 02 '15 at 17:31
  • Also, the plotting and plot list seem to be unrelated to your question. – Gregor Thomas Sep 02 '15 at 22:28
  • Hi Gregor. The output should be datafiles. jlhoward, the pltList is defined at the top of the code. Yes Gregor the plotting is unrelated I shall remove – Cam inacab Sep 03 '15 at 08:38
  • Then you need a line like `write.csv(x = PusungBarr, file = paste0("pusung", i, ".csv))`. in your loop. – Gregor Thomas Sep 03 '15 at 14:47

0 Answers0