Skip to content

Reading a file line by line - separating lines VS separating columns

2 messages · Tal Galili, jim holtman

#
You can do something like this using connections and read in a set of
lines and saving the results in bigmemory, or in this case a 'save'
image:

zz <- file("ex.data", "w") # open an output file
for (i in 1:10000)cat( "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t\t555\t\t",
file = zz, sep ="\n")
close(zz)

# read in the data 876 lines at a time and write out an image
zz <- file("ex.data", "r")
fileNo <- 1
repeat{
    gotError <- 1   # set to 2 if there is an error
    # catch the error if not more data
    tryCatch(input <- read.table(zz, nrows=876, sep='\t'),
error=function(x) gotError <<- 2)
    if (gotError == 2) break
    # save the intermediate data
    save(input, file=sprintf("file%03d.RDData", fileNo))
    fileNo <- fileNo + 1
}
close(zz)
On Wed, Mar 18, 2009 at 7:17 PM, Tal Galili <tal.galili at gmail.com> wrote: