How to create multiple ,csv files in R?

后端 未结 3 1581
礼貌的吻别
礼貌的吻别 2021-01-15 05:42

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 chrom

3条回答
  •  广开言路
    2021-01-15 05:50

    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.

提交回复
热议问题