How to create multiple ,csv files in R?

元气小坏坏 提交于 2019-12-01 08:14:24

问题


I have a .csv file with data for different chromosomes. The chromosomes names are stored in the first column(column name: Chr). My aim is to separate the data for each chromosome i.e. (Chr1,Chr2 etc) and make separate csv files for each. I cannot understand how to do this in limited steps. Thanks


回答1:


Illustrating a one liner using plyr and the dataset iris

plyr::d_ply(iris, .(Species), function(x) write.csv(x, 
  file = paste(x$Species, ".csv", sep = "")))



回答2:


  1. Read Data

    fn <- dir(pattern="csv")
    data_in <- do.call(rbind,lapply(fn,read.csv))
    
  2. Split by chromosome

    data_out <- split(data_in,data_in[[1]])
    
  3. Write by chromosome

    chn <- unlist(lapply(data_out,"[",1,1))
    for(i in seq_along(chn)) write.csv(data_out[[i]],file=paste(chn[i],"csv",sep="."))
    



回答3:


One way is to read the input file one line at a time and append the line to the correct outfile based on the first x characters of the line:

con <- file('yourInputFile', 'r') 

while (length(input <- readLines(con, n=1) > 0){ 
      outputfile <- paste(substr(input, 1, 5), ".csv", sep="" ) 
       ### assuming first 5 characters are new file name
      outfile <- file(outputfile, 'a')
      writeLines(output, con=outfile)
      close(outfile)
} 

The advantage of this approach is that it works even if yourInputFile is too big to read into memory. The downside is that this approach is slow as it does a lot of opening/closing of files.



来源:https://stackoverflow.com/questions/8126984/how-to-create-multiple-csv-files-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!